spine=2.11” The Visual Basic Book Evangelos Petroutsos MASTERING Programmers Count On MASTERING Microsoft® If you want to learn how to use Visual Basic 2010, you’ve come to the right V place. New users, you’ll build a simple VB application in the very fi rst chapter, getting you quickly up to speed on the VB user interface and simple coding. Intermediate and advanced users, you can bypass the basics and jump straight i to building data-driven applications, working with .NET 4.0 framework, creating rich client and web applications, programming with web services, Master Visual Basic 2010 s and much more. Packed with step-by-step instruction and real-world scenarios and .NET Framework 4.0 Microsoft® u for users of all levels, this book is what you need to build top-level skills in Visual Basic 2010 Visual Basic 2010. Understand the Core ® a COVERAGE INCLUDES: Language and User (cid:129) Getting started with Visual Basic 2010 Interface l (cid:129) Mastering VB programming essentials, including GUI design, event-driven programming, basic Windows controls, and more Find a Wealth of Content B (cid:129) Working with custom classes, controls, and objects for All Users (cid:129) Understanding the .NET framework, including XML, LINQ, and a storing data in collections Create Custom, Practical, (cid:129) Programming with ADO.NET, using the data entity model, and Data-Driven Applications s Build Rich Client and Work with the .NET understanding DataSets and Web Apps i (cid:129) Building web applications and programming with web services Web Applications with Framework 4.0 c (cid:129) Getting the most out of advanced Windows controls such as ListView Reinforce Your Skills with Visual Basic and TreeView controls Real-World Examples ® 2 ABOUT THE AUTHOR 0 Evangelos Petroutsos is a computer engineer who has worked for the California Institute of Technology and MCI. Currently, he writes 1 computer books and articles, teaches networking and programming courses, and works as a computer communications consultant. He is the author of the Sybex titles Mastering Microsoft Visual Basic 2008 and Mastering Microsoft Visual Basic 2005, among other books. 0 www.sybex.com/go/masteringvb2010 Petroutsos www.sybex.com ISBN 978-0-470-53287-4 CATEGORY COMPUTERS/Programming $59.99 US SERIOUS SKILLS. Languages/Visual Basic $71.99 CAN Mastering Microsoft Visual Basic 2010 ® ® Mastering Microsoft Visual Basic 2010 ® ® Evangelos Petroutsos Wiley Publishing, Inc. AcquisitionsEditor:AgathaKim DevelopmentEditor:MaryEllenSchutz TechnicalEditor:KirstinJuhl Production Editor:RachelMcConlogue Copy Editors:JudyFlynnandKim Wimpsett Editorial Manager:Pete Gaughan Production Manager:TimTate VicePresidentandExecutiveGroup Publisher:RichardSwadley VicePresidentandPublisher:NeilEdde Book Designers:MaureenForysandJudyFung Proofreader:RebeccaRider Indexer:JackLewis Project Coordinator, Cover:LynseyStanford Cover Designer:RyanSneed Cover Image:©PeteGardner/DigitalVision/GettyImages Copyright©2010by WileyPublishing,Inc.,Indianapolis,Indiana Publishedsimultaneously inCanada 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,scanningor otherwise,exceptaspermittedunderSections107 or108ofthe1976UnitedStatesCopyrightAct,withouteitherthepriorwrittenpermissionofthePublisher,orautho- 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. Requeststo thePublisher for permission should be addressed tothePermissionsDepartment,JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030,(201)748-6011,fax(201) 748-6008,or onlineat http://www.wiley.com/go/permissions. Limit of Liability/Disclaimerof Warranty:The publisher and the author make no representationsor warranties with respect to the accuracy or completenessof the contentsof this work and specifically disclaim all warranties, includ- ing without limitation warranties of fitness 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 engagedin rendering legal,accounting,or other professional services.If professional assistance is required,the servicesof a competentprofessional person should be sought. Neitherthe publisher nor the author shall be liable for damagesarising 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 changedor disappearedbetweenwhenthiswork waswrittenandwhenitisread. For general information on our other products and services or to obtain technical support, please contact our Cus- tomerCare DepartmentwithintheU.S.at (877) 762-2974,outside theU.S.at (317) 572-3993or fax (317)572-4002. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbeavail- able inelectronicbooks. Library ofCongressCataloging-in-PublicationData Petroutsos, Evangelos. MasteringMicrosoft Visual Basic2010 /EvangelosPetroutsos. --1st ed. p.cm. ISBN 978-0-470-53287-4(paper/website) 1.Microsoft Visual BASIC. 2.BASIC (Computerprogramlanguage)I.Title. QA76.73.B3P487 2010 005.2’768--dc22 2010000339 TRADEMARKS: Wiley,the Wileylogo, and the Sybex logoaretrademarksor registeredtrademarksof John Wiley& Sons,Inc.and/oritsaffiliates,intheUnitedStatesandothercountries,andmaynotbeusedwithoutwrittenpermis- sion.Microsoft andVisual Basicareregisteredtrademarksof MicrosoftCorporation intheUnitedStatesand/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated withanyproduct or vendormentionedin thisbook. 10987654321 DearReader, 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 experiencewith a giftforteaching. Sybex was founded in 1976.More than 30 years later, we’re still committed to producing con- sistently exceptional books. With each of our titles, we’re working hard to set a new standard fortheindustry. Fromthepaperweprint onto theauthorswe workwith, ourgoalis to bring youthebestbooksavailable. I hope you see all that reflected in these pages. I’d be very interested to hear your comments and get your feedback on how we’re doing. Feel free to let me know what you think about this oranyotherSybexbookbysending meanemailat [email protected]. If youthinkyou’ve founda technical errorin this book,please visit http://sybex.custhelp.com. Customerfeed- backiscritical tooureffortsat Sybex. Bestregards, NeilEdde VicePresidentandPublisher Sybex,anImprintofWiley To my dearest and most precious ones, Nepheli and Eleni-Myrsini Acknowledgments Manypeoplecontributedtothisbook,andIwouldlike tothankthemall.Ifirst wanttoexpress mydeepappreciation toDanijelArsenovski for contributingandrevising severalchapters,and especiallyforhisworkonChapter17,‘‘UsingtheEntityDataModel.’’Manythankstothebook’s technical editor, Kirstin Juhl, who has readthis bookwith great care and a particular attention todetail.Thankyou,Kirstin.IalsowanttothankthefolksatMicrosoftfortheircommitmentto Visual Basic. Visual Basic remainsmyabsolutefavoritelanguage. 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 workingonthisbooklong afterI’m donewiththis page).ToPete Gaughan,editorialmanager; Rachel McConlogue, production editor; Judy Flynn and Kim Wimpsett, copyeditors; Rebecca Rider, proofreader;JackLewis, indexer; thecompositorsat Laserwords;andeveryoneelse who addedtheirexpertiseandtalenttothisbook.