Visual Basic 2008 FOR DUMmIES by Bill Sempf
Visual Basic 2008 FOR DUMmIES
Visual Basic 2008 FOR DUMmIES by Bill Sempf
Visual Basic 2008 For Dummies Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright 2008 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. Visual Basic and Visual Studio are registered trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2008923124 ISBN: 978-0-470-18238-3 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1
About the Author I am Bill Sempf, and you ll notice that I don t write in third person. I have spent an inordinate amount of time in the last several years writing about, thinking about, and coding in VB.NET. I am a coauthor of Professional Visual Studio.NET, Effective Visual Studio.NET, Professional ASP.NET Web Services and Professional VB.NET (among others), and a frequent contributor to the Microsoft Developer Network, Builder.com, Hardcore Web Services, Inside Web Development Journal, and Intranet Journal. I have recently been an invited speaker for DevEssentials, the International XML Web Services Expo, and the Association of Information Technology Professionals. As a graduate of Ohio State University with a Bachelor of Science in Business Administration, Microsoft Certified Professional, Certified Internet Business Strategist, and Certified Internet Webmaster, I have developed over one hundred Web applications for startups and Fortune 500 companies alike. I began my career in 1985 by helping my father (also named Bill) manage Apple IIe systems for the local library. Since then, I have built applications for the likes of Lucent Technologies, Bank One, the State of Ohio, Nationwide Insurance, and Sears, Roebuck and Co. I specialized in data-driven Web applications of all types, both public and private. Currently, I am the Senior Technology Consultant at Products of Innovative New Technology in Grove City, Ohio, a Microsoft Certified Partner, working on a Learning Management System for OmniPath, inc. (www.omnipath.com). I can be reached at bill@pointweb.net.
Dedication This edition of the book was heavily influenced by the thousands of readers all over the world who took the time to e-mail with thoughts, questions, complaints, criticism, praise and ideas. To be honest, they are why I write. The majority of my good projects have come from people who read my books and call to collaborate. This book is dedicated to those readers. If you have taken the time to e-mail me or review the book or contact Wiley good or bad about this book series, thank you. Even if you are peeved because of a problem or error, you are helping make this a better product and that helps everyone. Despite what people often think, even with the best of editing and authoring, these books aren t perfect they are a community effort. Without that community, the books wouldn t be good or necessary. It s a self-replicating cycle, and I love it.
Author s Acknowledgments I cannot begin to thank the amazing team at Wiley who led me ever so carefully through the process of updating this book. Katie Feltman kept on me to revise the ideas I presented, and Mark Enochs saw to it that I stuck to schedule. The entire Wiley team, in fact, is the best an author could ever hope to have. I have gotten to know them well over the last several years, and I love them all. A project at Wendy s International led me to ask the most detail-oriented person I have ever met Mike Spivey to review the technical details of this edition. Jeff Simmons reviewed the original code, and did a good job, but this book is all the better for multiple sets of eyes. My army of peer reviewers from the original edition was fantastic: Theresa Alexander, Jim Andrews, David Deloveh, Rex Mahel, Greg McNamara, Rob Morgan, Blake Sparkes, and Gary Spencer. Here s a special note about my father, William E. Sempf, whose education background was of inestimable help in reviewing the early concepts for the book. Then, he let me use him as a guinea pig for Part I! What a trooper! Finally, a shout to the many Microsoft people who gave me a hand with specific questions about VB, Visual Studio, and the framework in general: Jan Shanahan and Susann Ragsdale in the Author Support Group, and Brad McCabe, Daniel Roth, Jay Roxe, and Steve Lasker, among many others, on the development teams.
Publisher s Acknowledgments We re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/. Some of the people who helped bring this book to market include the following: Acquisitions, Editorial, and Media Development Senior Project Editor: Mark Enochs Senior Acquisitions Editor: Katie Feltman Copy Editor: John Edwards Technical Editor: Mike Spivey Editorial Manager: Leah Cameron Media Development Project Manager: Laura Atkinson Editorial Assistant: Amanda Foxworth Sr. Editorial Assistant: Cherie Case Cartoons: Rich Tennant (www.the5thwave.com) Composition Services Project Coordinator: Kristie Rees Layout and Graphics: Alissa D. Ellet, Shane Johnson, Christine Williams, Erin Zeltner Proofreaders: John Greenough, Caitie Kelly Indexer: Potomac Indexing, LLC Publishing and Editorial for Technology Dummies Richard Swadley, Vice President and Executive Group Publisher Andy Cummings, Vice President and Publisher Mary Bednarek, Executive Acquisitions Director Mary C. Corder, Editorial Director Publishing for Consumer Dummies Diane Graves Steele, Vice President and Publisher Joyce Pepple, Acquisitions Director Composition Services Gerry Fahey, Vice President of Production Services Debbie Stailey, Director of Composition Services
Contents at a Glance Introduction...1 Part I: Getting to Know.NET Using Visual Basic...9 Chapter 1: Wading into Visual Basic...11 Chapter 2: Using Visual Studio 2008...23 Chapter 3: Designing Applications in VB 2008...43 Part II: Building Applications with VB 2008...59 Chapter 4: Building Windows Applications...61 Chapter 5: Building Web Applications...83 Chapter 6: Building Class Libraries...107 Chapter 7: Building Web Services...123 Chapter 8: Debugging in VB 2008...139 Part III: Making Your Programs Work...155 Chapter 9: Interpreting Strings and Things...157 Chapter 10: Making Decisions in Code...175 Chapter 11: Getting Loopy...187 Chapter 12: Reusing Code...197 Chapter 13: Making Arguments, Earning Returns...219 Part IV: Digging into the Framework...237 Chapter 14: Writing Secure Code...239 Chapter 15: Accessing Data...253 Chapter 16: Working with the File System...271 Chapter 17: Accessing the Internet...283 Chapter 18: Creating Images...297 Part V: The Part of Tens...309 Chapter 19: Ten Tips for Using the VB User Interface...311 Chapter 20: Ten Ideas for Taking Your Next Programming Step...325 Chapter 21: Ten Resources on the Internet...337 Index...343
Table of Contents Introduction...1 About This Book...2 Conventions Used in This Book...2 What You Don t Have to Read...3 Foolish Assumptions...4 How This Book Is Organized...4 Part I: Getting to Know.NET Using Visual Basic...4 Part II: Building Applications with VB 2008...5 Part III: Making Your Programs Work...5 Part IV: Digging into the Framework...5 Part V: The Part of Tens...5 Icons Used in This Book...6 Where to Go from Here...6 Part I: Getting to Know.NET Using Visual Basic...9 Chapter 1: Wading into Visual Basic............................11 Visual Basic s Role in the Framework...11 Saying Hello to VB 2008!...13 Installing Visual Studio...14 Starting a Windows Forms project...15 Adding functionality to the form with VB code...18 Running and operating your Windows form...19 Finding More Power in Visual Studio...20 Visual Studio doesn t just do Windows!...20 Visual Basic goes mobile...21 VB as your one-stop development shop...22 Chapter 2: Using Visual Studio 2008.............................23 Understanding Visual Studio Tools...23 Touring the Design View...24 Accessing controls with the Toolbox...26 Changing details with the Properties window...27 Organizing your project with the Solution Explorer...29 Accessing outside resources with the Server Explorer...30 Dynamically editing data with the Data Sources window...33 Moving a Tool Window...35