1058
SERIOUS SKILLS. Visual Basic ® 2010 MASTERING Build Rich Client and Web Applications with Visual Basic Work with the .NET Framework 4.0 Microsoft ® Evangelos Petroutsos

Mastering microsoft-visual-basic-2010

Embed Size (px)

DESCRIPTION

vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts vb book it explain all the concepts

Citation preview

  • 1. SERIOUS SKILLS. Visual Basic 2010 MASTERING Build Rich Client and Web Applications with Visual Basic Work with the .NET Framework 4.0 Microsoft Evangelos Petroutsos

2. Mastering Microsoft Visual Basic 2010 3. Mastering Microsoft Visual Basic 2010 Evangelos Petroutsos Wiley Publishing, Inc. 4. Acquisitions Editor: Agatha Kim Development Editor: Mary Ellen Schutz Technical Editor: Kirstin Juhl Production Editor: Rachel McConlogue Copy Editors: Judy Flynn and Kim Wimpsett Editorial Manager: Pete Gaughan Production Manager: Tim Tate Vice President and Executive Group Publisher: Richard Swadley Vice President and Publisher: Neil Edde Book Designers: Maureen Forys and Judy Fung Proofreader: Rebecca Rider Indexer: Jack Lewis Project Coordinator, Cover: Lynsey Stanford Cover Designer: Ryan Sneed Cover Image: Pete Gardner/DigitalVision/Getty Images Copyright 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-53287-4 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 autho- rization 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 Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specically disclaim all warranties, includ- ing without limitation warranties of tness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained 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 organiza- tion or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or rec- ommendations it may make. Further, readers should be aware that Internet Web sites 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 or to obtain technical support, please contact our Cus- tomer Care Department within the U.S. at (877) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be avail- able in electronic books. Library of Congress Cataloging-in-Publication Data Petroutsos, Evangelos. Mastering Microsoft Visual Basic 2010 / Evangelos Petroutsos. -- 1st ed. p. cm. ISBN 978-0-470-53287-4 (paper/website) 1. Microsoft Visual BASIC. 2. BASIC (Computer program language) I. Title. QA76.73.B3P487 2010 005.2768--dc22 2010000339 TRADEMARKS: Wiley, the Wiley logo, and the Sybex logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its afliates, in the United States and other countries, and may not be used without written permis- sion. Microsoft and Visual Basic 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. 10 9 8 7 6 5 4 3 2 1 5. Dear Reader, Thank you for choosing Mastering Microsoft Visual Basic 2010. This book is part of a family of premium-quality Sybex books, all of which are written by outstanding authors who combine practical experience with a gift for teaching. Sybex was founded in 1976. More than 30 years later, were still committed to producing con- sistently exceptional books. With each of our titles, were working hard to set a new standard for the industry. From the paper we print on to the authors we work with, our goal is to bring you the best books available. I hope you see all that reected in these pages. Id be very interested to hear your comments and get your feedback on how were doing. Feel free to let me know what you think about this or any other Sybex book by sending me an email at [email protected]. If you think youve found a technical error in this book, please visit http://sybex.custhelp.com. Customer feed- back is critical to our efforts at Sybex. Best regards, Neil Edde Vice President and Publisher Sybex, an Imprint of Wiley 6. To my dearest and most precious ones, Nepheli and Eleni-Myrsini 7. Acknowledgments Many people contributed to this book, and I would like to thank them all. I rst want to express my deep appreciation to Danijel Arsenovski for contributing and revising several chapters, and especially for his work on Chapter 17, Using the Entity Data Model. Many thanks to the books technical editor, Kirstin Juhl, who has read this book with great care and a particular attention to detail. Thank you, Kirstin. I also want to thank the folks at Microsoft for their commitment to Visual Basic. Visual Basic remains my absolute favorite language. Special thanks to the talented people at Sybex to all of them and to each one individ- ually starting with my Gentle Editor, Mary Ellen Schutz, who has taken this book under her wing and improved it in numerous ways. To acquisitions editor Agatha Kim, who has followed the progress of this book from its conception through its completion. (She will keep working on this book long after Im done with this page). To Pete Gaughan, editorial manager; Rachel McConlogue, production editor; Judy Flynn and Kim Wimpsett, copyeditors; Rebecca Rider, proofreader; Jack Lewis, indexer; the compositors at Laserwords; and everyone else who added their expertise and talent to this book. 8. About the Author Evangelos Petroutsos is a computer engineer by education, but he has spent most of his profes- sional life developing applications and digging through databases. He has a degree in computer engineering from the University of California, Santa Barbara, and several years of professional experience at the California Institute of Technology. He has worked as a consultant for many companies, large and small, and has taught courses on Visual Basic and databases. He espe- cially enjoys writing and teaching. With over 25 years of experience in this industry, he makes his living by optimizing code and databases. When hes not obsessed with a new technology, he spends time with his family and friends, reads science books, and nds excuses to visit every state in the country. 9. Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part 1 Visual Basic: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Getting Started with Visual Basic 2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2 Handling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Chapter 3 Visual Basic Programming Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Part 2 Developing Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 4 GUI Design and Event-Driven Programming . . . . . . . . . . . . . . . . . . . . 129 Chapter 5 Basic Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Chapter 6 Working with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Chapter 7 More Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Part 3 Working with Custom Classes and Controls . . . . . . . . . . . . . . . . . 303 Chapter 8 Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Chapter 9 Building Custom Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Chapter 10 Applied Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 387 Part 4 Working with the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . 431 Chapter 11 The Framework at Large . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Chapter 12 Storing Data in Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Chapter 13 XML in Modern Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 Chapter 14 An Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 10. xii CONTENTS AT A GLANCE Part 5 Developing Data-Driven Applications . . . . . . . . . . . . . . . . . . . . . . 629 Chapter 15 Programming with ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Chapter 16 Developing Data-Driven Applications . . . . . . . . . . . . . . . . . . . . . . . . 687 Chapter 17 Using the Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 Chapter 18 Building Data-Bound Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 Part 6 Developing for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813 Chapter 19 Accessing the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 Chapter 20 Building Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845 Chapter 21 Building and Using Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893 Appendix The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987 11. Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part 1 Visual Basic: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Getting Started with Visual Basic 2010 . . . . . . . . . . . . . . . . . . . . 3 Exploring the Integrated Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Start Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Starting a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Using the Windows Form Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Creating Your First VB Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Making the Application More User Friendly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Understanding the IDE Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The IDE Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Toolbox Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Solution Explorer Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Properties Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Output Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The Command and Immediate Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The Error List Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Setting Environment Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Building a Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Using Code Snippets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Using the My Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Chapter 2 Handling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Types of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 The Strict, Explicit, and Infer Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Variables as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Converting Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 User-Dened Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Examining Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 A Variables Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 A Variables Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 12. xiv CONTENTS Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Declaring Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Initializing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Chapter 3 Visual Basic Programming Essentials . . . . . . . . . . . . . . . . . . . 85 Flow-Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Decision Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Nested Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The Exit and Continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Writing and Using Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Argument-Passing Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Custom Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Passing Arguments and Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Overloading Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Part 2 Developing Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 4 GUI Design and Event-Driven Programming . . . . . . . . . . . . . . 129 On Designing Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Building a Loan Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Understanding How the Loan Calculator Application Works . . . . . . . . . . . . . . . . 131 Designing the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Programming the Loan Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Validating the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Building a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Designing the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Programming the MathCalculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Using the Basic Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Chapter 5 Basic Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 The TextBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Basic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Text-Manipulation Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Text-Selection Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Undoing Edits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 VB 2010 at Work: The TextPad Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 13. CONTENTS xv Capturing Keystrokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Autocomplete Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 The ListBox, CheckedListBox, and ComboBox Controls . . . . . . . . . . . . . . . . . . . . . . . 182 Basic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Manipulating the Items Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Selecting Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 VB 2010 at Work: The ListBox Demo Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Searching the ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 The ComboBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 The ScrollBar and TrackBar Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 The ScrollBar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 The TrackBar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Chapter 6 Working with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 The Appearance of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Properties of the Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Placing Controls on Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Setting the TabIndex Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 VB 2010 at Work: The Contacts Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Anchoring and Docking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Splitting Forms into Multiple Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Form Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Loading and Showing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 The Startup Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Controlling One Form from within Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Forms versus Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Building Dynamic Forms at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 The Forms Controls Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Creating Event Handlers at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Designing Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 The Menu Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 The ToolStripMenuItem Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Manipulating Menus at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Chapter 7 More Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 The Common Dialog Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Using the Common Dialog Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 The ColorDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 The FontDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 The OpenDialog and SaveDialog Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The FolderBrowserDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 The RichTextBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 The RTF Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Text Manipulation and Formatting Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 14. xvi CONTENTS Advanced Editing Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Cutting, Copying, and Pasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 VB 2010 at Work: The RTFPad Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 The TreeView and ListView Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Tree and List Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 The TreeView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 The ListView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 VB 2010 at Work: The CustomExplorer Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Part 3 Working with Custom Classes and Controls . . . . . . . . . . . . . . . . . . . . . . . 303 Chapter 8 Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 What Is a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Classes Combine Code with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Building the Minimal Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Adding Code to the Minimal Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Using Property Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Customizing Default Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Custom Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Object Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Using the SimpleClass in Other Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Firing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Instance and Shared Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 A Real Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Nesting Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 VB 2010 at Work: The LengthUnits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Chapter 9 Building Custom Windows Controls . . . . . . . . . . . . . . . . . . . 355 On Designing Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Enhancing Existing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Building the FocusedTextBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Building Compound Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 VB 2010 at Work: The ColorEdit Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Building User-Drawn Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 VB 2010 at Work: The Label3D Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Raising Custom Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Using the Custom Control in Other Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Designing Irregularly Shaped Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Customizing List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Designing Owner-Drawn ListBox Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 15. CONTENTS xvii Chapter 10 Applied Object-Oriented Programming . . . . . . . . . . . . . . . . 387 Issues in Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Classes versus Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Objects versus Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Properties versus Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 Shared versus Instance Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Early versus Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 Discovering a Variables Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 How to Apply Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Designing with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Building the Shape Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Who Can Inherit What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Parent Class Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Derived Class Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Parent Class Member Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Derived Class Member Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 VB 2010 At Work: The InheritanceKeywords Project . . . . . . . . . . . . . . . . . . . . . . . 420 MyBase and MyClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 Putting Inheritance to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 The Class Diagram Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 Part 4 Working with the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Chapter 11 The Framework at Large . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 What Is the Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Using Snippets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 Using the My Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 How to Use the My Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 The IO Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 The Directory Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 The File Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 The DriveInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 The DirectoryInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 The Path Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Streaming Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Drawing and Painting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Drawing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 The Image Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 16. xviii CONTENTS Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 The PrintDocument Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 The PrintDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 The PageSetupDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 The PrintPreviewDialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Page Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Basic Printing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 VB 2010 at Work: Generating a Simple Printout. . . . . . . . . . . . . . . . . . . . . . . . . . . 460 Handling Strings and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 The Char Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Handling Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 The DateTime Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 The TimeSpan Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 The StopWatch Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 Chapter 12 Storing Data in Collections . . . . . . . . . . . . . . . . . . . . . . . . . 493 Advanced Array Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 Searching Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 Performing Other Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498 Collection Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 Creating Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 Sorting Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Searching Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Iterating Through a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 The Dictionary Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 The HashTable Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 VB 2010 at Work: The WordFrequencies Project . . . . . . . . . . . . . . . . . . . . . . . . . . 513 The SortedList Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 Other Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519 The IEnumerator and IComparer Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519 Enumerating Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520 Custom Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 Chapter 13 XML in Modern Programming . . . . . . . . . . . . . . . . . . . . . . . 529 A Very Quick Introduction to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534 Numbers and Dates in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Manipulating XML with VB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 XML as a Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 Saving and Loading XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 17. CONTENTS xix Traversing XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 The Element and Elements Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 Ancestors and Descendants Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 Attribute Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 VB Axis Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 Editing XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 VB 2010 at Work: Manipulating XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546 Locating Information in the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Editing the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 Using XML Segments as Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 Using Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 XML Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 The Serialization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Serializing Individual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 Serializing Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 Serializing Collections of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 Other Types of Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Deserializing Individual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Chapter 14 An Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 What Is LINQ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 LINQ Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 LINQ to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Anonymous Types and Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 Querying Arbitrary Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 Aggregating with LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 Some Practical LINQ Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 Transforming Objects with LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 LINQ to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 Adding Dynamic Content to an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . 599 LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609 Retrieving Data with the ExecuteQuery Method . . . . . . . . . . . . . . . . . . . . . . . . . . 613 Working with LINQ to SQL Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 Navigation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628 Part 5 Developing Data-Driven Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 Chapter 15 Programming with ADO.NET . . . . . . . . . . . . . . . . . . . . . . . 631 What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Using Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 Obtaining the Northwind and Pubs Sample Databases . . . . . . . . . . . . . . . . . . . . . 633 18. xx CONTENTS Exploring the Northwind Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 Exploring the Pubs Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Understanding Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640 SQL: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642 Executing SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 Selection Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 Working with Calculated Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 Calculating Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 Using SQL Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 Grouping Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 Action Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658 Deleting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659 Inserting New Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Editing Existing Rows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 Stream- versus Set-Based Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 The Basic Data-Access Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 The Connection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 The Command Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 The DataReader Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 Chapter 16 Developing Data-Driven Applications . . . . . . . . . . . . . . . . . . 687 Using Business Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 VB 2010 at Work: The NWOrders Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 Storing Data in DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 Filling DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702 Accessing the DataSets Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 Working with Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 Handling Null Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709 Adding and Deleting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710 Navigating Through a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711 Performing Update Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 Updating the Database with the DataAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 Handling Identity Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716 VB 2010 at Work: The SimpleDataSet Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 Chapter 17 Using the Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . 725 The Entity Framework: Raising the Data Abstraction Bar . . . . . . . . . . . . . . . . . . . . . 725 How Will You Benet from the Entity Framework? . . . . . . . . . . . . . . . . . . . . . . . 726 Entity Data Model: Model-First Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 Putting the EDM to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 Querying the Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 Modifying the Data with the Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . . 763 Reverse-Engineering an Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 19. CONTENTS xxi Chapter 18 Building Data-Bound Applications . . . . . . . . . . . . . . . . . . . . 769 Working with Typed DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 Generating a Typed DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770 Exploring the Typed DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 Using the BindingSource Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781 Designing Data-Driven Interfaces the Easy Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786 Enhancing the Navigational Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789 Binding Hierarchical Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 Adjusting the Appearance of the DataGridView Control . . . . . . . . . . . . . . . . . . . 794 Editing the Data in Hierarchical Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 Building More-Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 Data Binding with LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811 Part 6 Developing for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813 Chapter 19 Accessing the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 The WebBrowser Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 WebBrowser Control under the Hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 WebBrowser Control Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 WebBrowser Control Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821 WebBrowser Control Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822 VB 2010 at Work: The Stock Quotes Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823 Accessing the Web with the WebClient and HttpWebRequest/Response Classes . . . 827 The WebClient Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 WebClient Class Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 WebClient Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828 WebClient Class Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829 WebClient Asynchronous Download Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 830 HttpWebRequest and HttpWebResponse Classes . . . . . . . . . . . . . . . . . . . . . . . . . 831 Putting It All Together: The Address Visualization Form . . . . . . . . . . . . . . . . . . . . . . 831 Composing Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832 Coding Address Visualization Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842 Chapter 20 Building Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . 845 Developing for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845 Understanding HTML and XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846 Working with HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848 Page Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848 Text Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 Horizontal Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 20. xxii CONTENTS Embedding Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852 Page Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 Forms and Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854 Cascading Style Sheets (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856 Formatting Styles with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 Page Formatting with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858 JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861 AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Microformats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Server-Side Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Creating a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867 Standard Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867 Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868 Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868 Navigation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868 Login Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 WebParts Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 AJAX Extensions Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 Reporting Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871 HTML Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871 Maintaining State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871 Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874 ASP.NET Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875 Postback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 VB 2010 at Work: Online Ordering Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880 Creating the Products Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880 Creating the Quantity Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891 Chapter 21 Building and Using Web Services . . . . . . . . . . . . . . . . . . . . . 893 Using ASP.NET and WCF Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893 What Is a Service? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894 Consuming Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894 ASP.NET Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898 WCF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899 Understanding Technologies Associated with Web Services . . . . . . . . . . . . . . . . . . . 899 SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899 WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900 SOAP Discovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900 UDDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900 21. CONTENTS xxiii Creating a Simple ASP.NET Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900 Setting Up the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901 Testing the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901 Consuming the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902 Developing a Stand-Alone Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903 Building MyWebService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904 Deploying MyWebService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905 Consuming MyWebService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906 Simple AJAX Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910 Building and Using WCF Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912 Building a WCF Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912 ADO.NET Data Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920 Building a Windows Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928 Submitting Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932 Performing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934 Securing Your Data Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937 The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939 Appendix The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 Chapter 1: Getting Started with Visual Basic 2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 Chapter 2: Handling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942 Chapter 3: Visual Basic Programming Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944 Chapter 4: GUI Design and Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . 945 Chapter 5: Basic Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946 Chapter 6: Working with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948 Chapter 7: More Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950 Chapter 8: Working with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953 Chapter 9: Building Custom Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956 Chapter 10: Applied Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . 959 Chapter 11: The Framework at Large . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959 Chapter 12: Storing Data in Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 Chapter 13: XML in Modern Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967 Chapter 14: An Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970 Chapter 15: Programming with ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 972 Chapter 16: Developing Data-Driven Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 974 Chapter 17: Using the Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975 Chapter 18: Building Data-Bound Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976 Chapter 19: Accessing the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978 Chapter 20: Building Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981 Chapter 21: Building and Using Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987 22. Introduction Welcome to Microsofts Visual Basic 2010, another milestone version of the most popular programming language for building Windows and web applications. In modern software devel- opment, however, the language is only one of the components we use to build applications. The most important component is the .NET Framework, which is an indispensable component of every application; its actually more important than the language itself. You can think of the Framework as an enormous collection of functions for just about any programming task. All drawing methods, for example, are part of the System.Drawing class. To draw a rectangle, you call the DrawRectangle method of the System.Drawing class, passing the appropriate arguments. To create a new folder, you call the CreateDirectory method of the Directory class, and to retrieve the les in a folder, you call the GetFiles method of the same class. The Framework contains all the functionality of the operating system and makes it available to your application through methods. Methods are very similar to functions, which extend the basic capabilities of a language. The Framework is a huge collection of such methods, organized in units according to their role and in a way that makes it fairly easy to locate the methods for the task at hand. The language and the Framework are the two programming components absolutely necessary to build Windows applications. Its possible to develop applications with these two components alone, but the process would be awfully slow. The software development process relies on numerous tools that streamline the coding expe- rience. The third component is an integrated environment that hosts those tools, enabling you to perform many common tasks with point-and-click operations. Its basically an environment in which you can design your forms with visual tools and write code as well. This environ- ment, provided by Visual Studio, is known as an integrated development environment, or IDE. Youll be amazed by the functionality provided by the tools of Visual Studio: you can actu- ally design a functional data-driven application without writing a single line of code. You can use similar tools in the same environment to design a fancy data-driven web page without a single line of code. Visual Studio even provides tools for manipulating databases and allows you to switch between tasks, all in the same, streamlined environment. You realize, of course, that Visual Studio isnt about writing applications without code; it just simplies certain tasks through wizards, and more often than not, we step in and provide custom code to write a functional application. Even so, Visual Studio provides numerous tools, from debugging tools that help you track and x all kinds of bugs in your code to database-manipulation tools and deployment wizards that streamline the process of deploying applications. This book shows you how to use Visual Studio 2010 and Visual Basic 2010 to design rich Windows and web applications. Well start with the visual tools and then well explore Visual Basic and the Framework. A Windows application consists of a visual interface and code behind the elements of the interface. (The code handles the user actions on the visual interface, such as the click of a button, the selection of a menu item, and so on.) Youll use the 23. xxvi INTRODUCTION tools of Visual Studio to build the visual interface, and then youll program the elements of the application with Visual Basic. For any nontrivial processing, such as le and folder manip- ulation, data storage, and so on, youll use the appropriate classes of the .NET Framework. A substantial segment of this book deals with the most useful components of the Framework. We will also explore databases and data-driven applications, which are the most common type of business applications. Finally, well go through the basics of web programming. Youll learn how to build web applications with Visual Basic and how to write web services. The Mastering Series The Mastering series from Sybex provides outstanding instruction for readers with intermedi- ate and advanced skills in the form of top-notch training and development for those already working in their eld and clear, serious education for those aspiring to become pros. Every Mastering book includes the following: Real-World Scenarios, ranging from case studies to interviews, that show how the tool, technique, or knowledge presented is applied in actual practice Skill-based instruction, with chapters organized around real tasks rather than abstract concepts or subjects Self-review test questions, so you can be certain youre equipped to do the job right Who Should Read This Book? You dont need a solid knowledge of Visual Basic to read this book, but you do need a basic understanding of programming. You need to know the meaning of variables and functions and how an IfThen structure works. This book is aimed at the typical programmer who wants to get the most out of Visual Basic. It covers the topics I felt are of use to most VB programmers, and it does so in depth. Visual Basic 2010 and the .NET Framework 4.0 are two extremely rich programming tools, and I had to choose between a supercial coverage of many topics and an in-depth coverage of fewer topics. To make room for more topics, I have avoided including a lot of reference material and lengthy listings. For example, you wont nd complete project listings or form descriptions. I assume that you can draw a few controls on a form and set their properties and that you dont need long descriptions of the control properties (even if you dont know how to design a form, youll learn how in the rst two chapters). Im also assuming that you dont want to read the trivial segments of each application. Instead, the listings concentrate on the meaty part of the code: the procedures that explain the topic at hand. The topics covered in this book were chosen to provide a solid understanding of the prin- ciples and techniques for developing applications with Visual Basic. Programming isnt about new keywords and functions. I chose the topics I felt every programmer should learn in order to master the language. I was also motivated by my desire to present useful, practical examples. You will not nd all topics equally interesting or important. My hope is that everyone will nd something interesting and something of value for their daily work whether its an applica- tion that maps the folders and les of a drive to a TreeView control, an application that prints tabular data, a data-driven application for editing customers or products, or an application that saves a collection of objects to a le. Many books offer their readers long, numbered sequences of steps to accomplish a task. Fol- lowing instructions simplies certain tasks, but programming isnt about following instructions. 24. INTRODUCTION xxvii Its about being creative; its about understanding principles and being able to apply the same techniques in several practical situations. And the way to creatively exploit the power of a lan- guage such as Visual Basic 2010 is to understand its principles and its programming model. In many cases, I provide a detailed, step-by-step procedure that will help you accomplish a task, such as designing a menu, for example. But not all tasks are as simple as designing menus. I explain why things must be done in a certain way, and I present alternatives and try to connect new topics to those explained earlier in the book. In several chapters, I expand on applications developed in earlier chapters. Associating new knowledge with something you have mastered already provides positive feedback and a deeper understanding of the language. This book isnt about the hottest features of the language either; its about solid program- ming techniques and practical examples. After you master the basics of programming Windows applications with Visual Basic 2010 and you feel comfortable with the more advanced examples of the book, you will nd it easy to catch up with the topics not discussed in this book. How about the Advanced Topics? Some of the topics discussed in this book are nontrivial, and quite a few topics can be consid- ered advanced. Creating collections of custom objects and querying them and exposing some functionality in the form of web services are not trivial topics, but these are the tools that will allow you to make the most of Visual Studio. You may also nd some examples to be more difcult than you expected. I have tried to make the text and the examples easy to read and understand, but not unrealistically simple. Understanding the basic functions for manipulating les and folders isnt difcult. To make the most of these functions, however, you need to understand how to scan a folders les, includ- ing the les in its subfolders and the les in their subfolders, with a technique known as recur- sion. To make each chapter as useful as possible, Ive included nontrivial examples, which will provide a better understanding of the topics. In addition, many of these examples can be easily incorporated into your applications. You can do a lot with the TreeView control with very little programming, but to make the most out of this control, you must be ready for some advanced programming nothing terri- bly complicated, but some things just arent trivial. Programming most of the operations of the TreeView control, for instance, is not complicated, but if your application calls for populating a TreeView control with an arbitrary number of branches (such as mapping a directory structure to a TreeView control), the code can get complex. The same goes for printing; its fairly straightforward to write a program that prints some text, but printing tabular reports takes substantial coding effort. The reason Ive included the more advanced examples is that the corresponding chapters would be incomplete without them. If you nd some material to be over your head at rst reading, you can skip it and come back to it after you have mastered other aspects of the lan- guage. But dont let a few advanced examples intimidate you. Most of the techniques are well within the reach of an average VB programmer. The few advanced topics were included for the readers who are willing to take that extra step and build elaborate interfaces by using the latest tools and techniques. Theres another good reason for including advanced topics. Explaining a simple topic, such as how to populate a collection with items, is very simple. But what good is it to populate a collection if you dont know how to save it to disk and read back its items in a later session? Likewise, what good is it to learn how to print simple text les? In a business environment, you will most likely be asked to print a tabular report, which is substantially more complicated 25. xxviii INTRODUCTION than printing text. One of my goals in writing this book was to exhaust the topics Ive chosen to discuss and present all the information you need to do something practical: not just how to create collections, but also how to save them in disk les; not just how to write to a le, but also how to prompt users for a lename with the same dialog box all Windows applications use; not just how to print something, but also how to create a preview of the printout. In short, Ive tried to include everything you need to know in order to incorporate in your applications the features everybody has come to expect from a Windows application. The Structure of the Book This book isnt meant to be read from cover to cover, and I know that most people dont read computer books this way. Each chapter is independent of the others, although all chapters con- tain references to other chapters. Each topic is covered in depth; however, I make no assump- tions about the readers knowledge of the topic. As a result, you may nd the introductory sections of a chapter too simple. The topics become progressively more advanced, and even experienced programmers will nd some new information in most chapters. Even if you are familiar with the topics in a chapter, take a look at the examples. I have tried to simplify many of the advanced topics and to demonstrate them with clear, practical examples. This book tries to teach through examples. Isolated topics are demonstrated with short examples, and at the end of many chapters youll build a large, practical application (a real- world application) that puts together the topics and techniques discussed throughout the chapter. You may nd some of the more advanced applications a bit more difcult to under- stand, but you shouldnt give up. Simpler applications would have made my job easier, but the book wouldnt deserve the Mastering title, and your knowledge of Visual Basic wouldnt be as complete. The book starts with the fundamentals of Visual Basic, even though very little of it is specic to version 2010. Youll learn how to design visual interfaces with point-and-click operations and how to program a few simple events, such as the click of the mouse on a button. After reading the rst two chapters, youll understand the structure of a Windows application. Then youll explore the elements of the visual interface (the basic Windows controls) and how to program them. Youll also learn about the My object and code snippets, two features that make Visual Basic so simple and fun to use (again). These two objects will also ease the learning process and make it much simpler to learn the features of the language. In Part 2, I discuss in detail the basic components of Windows applications. I explain the most common controls youll use in building Windows forms as well as how to work with forms: how to design forms, how to design menus for your forms, how to create applications with multiple forms, and so on. You will nd detailed discussions of many Windows controls as well as how to take advantage of the built-in dialog boxes, such as the Font and Color dialog boxes, in your applications. Visual Basic 2010 is a truly object-oriented language, and objects are the recurring theme in every chapter. Part 3 of the book (Chapter 8, Chapter 9, and Chapter 10) contains a formal and more systematic treatment of objects. You will learn how to build custom classes and controls, which will help you understand object-oriented programming a little better. You will also learn about inheritance and will see how easy it is to add custom functionality to existing classes through inheritance. Part 4 deals with some of the most common classes of the .NET Framework. The Frame- work is at the very heart of Windows programming; its your gateway to the functionality of the operating system itself. The rst chapter in this part of the book is an introduction to the 26. INTRODUCTION xxix Framework at large, and it shows you how to use the basic classes for manipulating les and folders, how to manipulate data and time, how to work with time spans, how to create graph- ics and printouts, and other interesting aspects of the Framework. In the next chapter youll learn how to use collections in your code and then youll nd a chapter on XML and a chapter on LINQ. You will see how easy it is to create and use XML in your VB code as well as how to query collections, XML, and databases with a new language thats embedded into VB: Lan- guage Integrated Query (LINQ). LINQ is the hottest new technology that allows you to query data with widely different structures, and data from different sources, in a uniform way. The rst 14 chapters deal with the fundamentals of the language and Windows applications. Following these chapters, you will nd an overview of the data-access tools. Im assuming that the majority of you will eventually build a data-driven application. The emphasis in Part 5 is on the visual tools, and you will learn how to query databases and present data to the user. You will also nd information on programming the basic objects of ADO.NET and write simple data-driven Windows applications. In the last few chapters of this book you will learn about web applications, the basics of ASP.NET 4, how to develop data-bound web applications, and how to write web services. Since I could not discuss both Windows and web applications in the same detail, Ive decided to focus on Windows applications, and in the last few chapters (Part 6) show you how to apply your knowledge to the Web. While the interface is totally different, the essential code is the same. Dont Miss the Tutorials In addition to the printed material, this book is accompanied by a number of tutorials, which you can download from www.sybex.com/go/masteringvb2010. These tutorials are actual chap- ters (some of them quite lengthy); we couldnt include them in the printed version of the book, so we included them as PDF les. They are as follows: Accessing Files and Folders Creating Graphics with VB 2010 Printing with VB 2010 Making the Most of the ListView and TreeView Controls This book is a revision of Mastering Visual Basic 2008. As the book couldnt keep growing and we had to make room for new topics we decided to remove some chapters that were included in the previous edition of the book from the printed version. These chapters have been revised and edited and you will nd them in PDF format at this books website. Throughout this book, Ill be referring to them as tutorials; theyre complete chapters with sample projects and the same structure as the books chapters. You can download the tutorials from the same site as the books projects and read them on your computer screen. Downloading This Books Code The code for the examples and projects can be downloaded from the Sybex website (www. sybex.com). At the main page, you can nd the books page by searching for the author, the title, or the ISBN (9780470187425) and then clicking the books link listed in the search results. On the books page, click the Download link and it will take you to the download 27. xxx INTRODUCTION page. Or, you can go directly to the books page at www.sybex.com/go/masteringvb2010. The downloaded source code is a ZIP le, which you can unzip with the WinZip utility. How to Reach the Author Despite our best efforts, a book of this size is bound to contain errors. Although a printed medium isnt as easy to update as a website, I will spare no effort to x every problem you report (or I discover). The revised applications, along with any other material I think will be of use to the readers of this book, will be posted on the Sybex website. If you have any problems with the text or the applications in this book, you can contact me directly at [email protected]. Although I cant promise a response to every question, I will x any problems in the examples and provide updated versions. I would also like to hear any comments you may have on the book, about the topics you liked or did not like and how useful the examples are. Your comments will be carefully considered for future editions. 28. Mastering Microsoft Visual Basic 2010 29. Part 1 Visual Basic: The Language Chapter 1: Getting Started with Visual Basic 2010 Chapter 2: Handling Data Chapter 3: Visual Basic Programming Essentials 30. Chapter 1 Getting Started with Visual Basic 2010 Im assuming that you have installed one of the several versions of Visual Studio 2010. For this book, I used the Professional edition of Visual Studio, but just about everything discussed in this book applies to the Standard edition as well. Some of the Professional edition features that are not supported by the Standard edition include the database tools, which are discussed in Chapter 15 through Chapter 18 of this book. You may have already explored the new environment on your own, but Im going to start with an overview of Visual Studio and its basic tools for the benet of readers who arent famil- iar with them. I will not assume any prior knowledge of Visual Basic 6 or Visual Basic .NET, just some familiarity with programming at large. As you already know, Visual Basic 2010 is just one of the languages you can use to build applications with Visual Studio 2010. I happen to be convinced that it is also the simplest, most convenient language, but this isnt really the issue; Im assuming you have your reasons to code in VB or you wouldnt be reading this book. What you should keep in mind is that Visual Stu- dio 2010 is an integrated environment for building, testing, debugging, and deploying a vari- ety of applications: Windows applications, web applications, classes and custom controls, and even console applications. It provides numerous tools for automating the development process, visual tools for performing many common design and programming tasks, and more features than any author could hope to cover. In this chapter, youll learn how to do the following: Navigate the integrated development environment of Visual Studio Understand the basics of a Windows application Exploring the Integrated Development Environment Visual Basic 2010 is just one of the languages you can use to program your applications. The language is only one aspect of a Windows application. The visual interface of the application isnt tied to a specic language, and the same tools youll use to develop your applications interface will also be used by all programmers, regardless of the language theyll use to code the application. 31. 4 CHAPTER 1 GETTING STARTED WITH VISUAL BASIC 2010 To simplify the process of application development, Visual Studio provides an environment thats common to all languages, known as an integrated development environment (IDE). The pur- pose of the IDE is to enable the developer to do as much as possible with visual tools before writing code. Even as you write code, the IDE will help you in many ways. For example, it underlines errors, it suggests the keywords that may appear at the current place in your code in a list, and it even provides tools for locating and xing errors (a process known as debugging). The IDE provides tools for designing, executing, and debugging your applications. It will be a while before you explore all the elements of the IDE, and I will explain the various items as needed in the course of the book. In the following sections, youll look at the basic components of the IDE youll be using to build simple Windows applications. Youll learn how its tools allow you to quickly design the user interface of your application as well as how to program the application. The IDE is your second desktop, and youll be spending most of your productive hours in this environment. The Start Page When you run Visual Studio 2010 for the rst time, you will be prompted to select the type of projects you plan to build so that the environment can be optimized for that specic type of development. Im assuming that you have initially selected the Visual Basic Development settings, which will optimize your copy of Visual Studio for building Windows and web appli- cations with Visual Basic 2010. You can always change these settings, as explained at the end of this section. After the initial conguration, you will see a window similar to the one shown in Figure 1.1. The Recent Projects tab will be empty, of course, unless you have already created some test projects. Visual Studio 2010 will detect the settings of a previous installation, so if youre upgrading from an earlier version of Visual Studio, the initial screen will not be identical to the one shown in Figure 1.1. Figure 1.1 This is what youll see when you start Visual Studio for the rst time. 32. EXPLORING THE INTEGRATED DEVELOPMENT ENVIRONMENT 5 On the Start Page window of Visual Studio, you will see the following panes under the Get Started heading: Welcome Click the Welcome tab to see a series of links that provide developer assistance for using Visual Studio. These links include Whats New In Visual Studio 2010, Creating Applica- tions With Visual Studio, and Extending Visual Studio, among others. Other related links may be added as this book goes to the printer. Windows Here youll nd a list of topics related to Windows application development. Win- dows applications, frequently referred to as desktop applications, are the applications you install on a local computer and execute locally. Web Here youll nd a list of topics related to web application development. Web applica- tions are executed on a remote computer, the web server, and you interact with them through a browser. Cloud, Ofce, SharePoint In addition to Windows and web applications, Visual Studio can be used to develop applications for Ofce and SharePoint as well as applications that use a new Microsoft platform for building distributed applications, the Azure platform. These three types of projects arent discussed in this book. Data Here youll nd a list of topics related to data-driven programming. All applications that interact with a database are data driven; they can be Windows or web applications. The principles of interacting with a database (retrieve, display, and update database data) are the same regardless of whether you use them to build Windows or web applications. Recent Projects Here you see a list of the projects you opened most recently with Visual Stu- dio, and you can select the one you want to open again chances are you will continue work- ing on the same project as the last time. Each project name is a hyperlink, and you can open a