ebook img

Clearly Visual Basic: Programming with Microsoft Visual Basic 2010 PDF

594 Pages·2011·12.59 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Clearly Visual Basic: Programming with Microsoft Visual Basic 2010

This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest. CLEARLY VISUAL BASIC ® PROGRAMMING WITH MICROSOFT® VISUAL BASIC® 2010 This page intentionally left blank SECOND EDITION CLEARLY VISUAL BASIC® PROGRAMMING WITH MICROSOFT® VISUAL BASIC® 2010 DIANE ZAK AustralialBrazillJapanlKorealMexicolSingaporelSpainlUnitedKingdomlUnitedStates ClearlyVisualBasic: ª2012CourseTechnology,CengageLearning ProgrammingwithMicrosoft ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyrighthereinmaybe VisualBasic2010,SecondEdition reproduced,transmitted,storedorusedinanyformorbyanymeansgraphic,electronic, DianeZak ormechanical,includingbutnotlimitedtophotocopying,recording,scanning,digitizing, taping,Webdistribution,informationnetworks,orinformationstorageandretrieval ExecutiveEditor:MarieLee systems,exceptaspermittedunderSection107or108ofthe1976UnitedStates AcquisitionsEditor:BrandiShailer CopyrightAct,withoutthepriorwrittenpermissionofthepublisher. SeniorProductManager:AlyssaPratt FreelanceProductManager:TriciaCoia Forproductinformationandtechnologyassistance,contactusat AssociateProductManager: CengageLearningCustomer&SalesSupport,1-800-354-9706 StephanieLorenz Forpermissiontousematerialfromthistextorproduct, MarketingManager:ShannaShelton submitallrequestsonlineatcengage.com/permissions SeniorContentProjectManager:JillBraiewa Furtherpermissionsquestionscanbeemailedto QualityAssurance:GreenPenQA [email protected] ArtDirector:FaithBrosnan CoverDesigner:CabbageDesignCompany LibraryofCongressControlNumber:2011921829 TextDesigner:ShawnGirsberger ISBN-13:978-1-111-53015-0 PrintBuyer:JulioEsperas ISBN-10:1-111-53015-7 Proofreader:SuzanneHuizenga Indexer:AlexandraNickerson CourseTechnology Compositor:IntegraSoftwareServices 20ChannelCenterStreet Boston,MA02210 USA Someoftheproductnamesandcompanynamesusedinthisbookhavebeenusedfor identificationpurposesonlyandmaybetrademarksorregisteredtrademarksoftheir respectivemanufacturersandsellers. CourseTechnology,apartofCengageLearning,reservestherighttorevisethis publicationandmakechangesfromtimetotimeinitscontentwithoutnotice. Example:Microsoft®isaregisteredtrademarkoftheMicrosoftCorporation. CengageLearningisaleadingproviderofcustomizedlearningsolutionswithoffice locationsaroundtheglobe,includingSingapore,theUnitedKingdom,Australia, Mexico,Brazil,andJapan.Locateyourlocalofficeat:www.cengage.com/global CengageLearningproductsarerepresentedinCanadabyNelsonEducation,Ltd. TolearnmoreaboutCourseTechnology,visit www.cengage.com/coursetechnology TolearnmoreaboutCengageLearning,visitwww.cengage.com Purchaseanyofourproductsatyourlocalcollegestoreoratourpreferredonlinestore www.cengagebrain.com Printed in the United States of America 1 2 3 4 5 6 7 17 16 15 14 13 12 11 v Brief Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . xiv CHAPTER 1 I Am Not a Control Freak! (Control Structures) . . . . . . . . . . 1 CHAPTER 2 First You Need to Plan the Party (Problem-Solving Process) . . . 11 CHAPTER 3 I Need a Tour Guide (Introduction to Visual Basic 2010) . . . . . 27 CHAPTER 4 Do It Yourself Designing (Designing Interfaces) . . . . . . . . . 49 CHAPTER 5 The Secret Code (Assignment Statements) . . . . . . . . . . . 67 CHAPTER 6 Where Can I Store This? (Variables and Constants) . . . . . . . 81 CHAPTER 7 What’s Wrong with It? (Syntax and Logic Errors) . . . . . . . . .101 CHAPTER 8 Decisions, Decisions, Decisions (Selection Structure) . . . . . .115 CHAPTER 9 Time to Leave the Nest (Nested Selection Structures) . . . . . .139 CHAPTER 10 So Many Paths . . . So Little Time (Multiple-Alternative Selection Structures) . . . . . . . . . . . . . . . . . . . .159 CHAPTER 11 Testing, Testing . . . 1, 2, 3 (Selecting Test Data) . . . . . . . .183 CHAPTER 12 How Long Can This Go On? (Pretest Loops) . . . . . . . . . . .203 CHAPTER 13 Do It, Then Ask Permission (Posttest Loops) . . . . . . . . . .231 CHAPTER 14 Let Me Count the Ways (Counter-Controlled Loops) . . . . . . .241 CHAPTER 15 I’m on the Inside; You’re on the Outside (Nested Loops) . . . . .261 CHAPTER 16 I Hear You Are Breaking Up (Sub Procedures) . . . . . . . . . .279 CHAPTER 17 Talk to Me (Function Procedures) . . . . . . . . . . . . . . .303 CHAPTER 18 A Ray of Sunshine (One-Dimensional Arrays) . . . . . . . . . .319 CHAPTER 19 Parallel and Dynamic Universes (More on One-Dimensional Arrays) . . . . . . . . . . . . . . . . . . . . . . . . . . .341 CHAPTER 20 Table Tennis, Anyone? (Two-Dimensional Arrays) . . . . . . . .363 CHAPTER 21 Building Your Own Structure (Structures) . . . . . . . . . . . .383 vi CONTENTS CHAPTER 22 I’m Saving for the Future (Sequential Access Files) . . . . . . .399 CHAPTER 23 The String Section (String Manipulation) . . . . . . . . . . . .417 CHAPTER 24 I’m Suffering from Information Overload (Access Databases) . . .439 CHAPTER 25 The Missing “LINQ” (Querying a Database) . . . . . . . . . . .461 CHAPTER 26 I Love This Class (Creating a Class) . . . . . . . . . . . . . .475 CHAPTER 27 Getting “Web-ified” (Web Applications) . . . . . . . . . . . . .497 APPENDIX A Data Types . . . . . . . . . . . . . . . . . . . . . . . . .527 APPENDIX B Answers to Mini-Quizzes and TRY THIS Exercises . . . . . . . .529 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . .569 vii Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv CHAPTER 1 I Am Not a Control Freak! (Control Structures) . . . . . . . . . . . 1 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 The Sequence Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 The Selection Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 The Repetition Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 CHAPTER 2 First You Need to Plan the Party (Problem-Solving Process) . . . . 11 How Do Programmers Solve Problems? . . . . . . . . . . . . . . . . . . . . . . . . 12 Step 1—Analyze the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Step 2—Plan the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Step 3—Desk-Check the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 CHAPTER 3 I Need a Tour Guide (Introduction to Visual Basic 2010) . . . . . . 27 Ok, the Algorithm Is Correct. What’s Next? . . . . . . . . . . . . . . . . . . . . . . . 28 Creating a Visual Basic Windows Application . . . . . . . . . . . . . . . . . . . . . . 29 So Many Windows! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Creating the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Save, Save, Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Whose Property Is It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Using the Format Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Lock Them Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Ok, Let’s See the Interface in Action! . . . . . . . . . . . . . . . . . . . . . . . . . 41 Closing the Current Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Opening an Existing Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Exiting Visual Studio 2010 or Visual Basic 2010 Express . . . . . . . . . . . . . . . . . 43 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 viii CONTENTS CHAPTER 4 Do It Yourself Designing (Designing Interfaces) . . . . . . . . . 49 Delegating the Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Making the Interface More User-Friendly . . . . . . . . . . . . . . . . . . . . . . . . 54 Do What I Tell You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Me.Close() Instruction . . . . . . . . . . . . . . . . . . . . . . . . . 58 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 CHAPTER 5 The Secret Code (Assignment Statements) . . . . . . . . . . . 67 The Fun Starts Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Val Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Who’s in Charge of This Operation? . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Your Assignment, if You Choose to Accept It . . . . . . . . . . . . . . . . . . . . . . 72 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 CHAPTER 6 Where Can I Store This? (Variables and Constants) . . . . . . . . 81 Using Storage Bins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 So, What’s Your Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Let’s Play the Name Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 You’ll Need a Reservation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 How Many Variables Should I Use? . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The TryParse Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Check, Please…I’m Ready to Go . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Using Constants to Keep Things…Well, the Same . . . . . . . . . . . . . . . . . . . . 89 Dressing Up the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 CHAPTER 7 What’s Wrong with It? (Syntax and Logic Errors) . . . . . . . . .101 There’s a Bug in My Soup! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Finding Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Locating Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 I’ve Reached My Breaking Point . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 CHAPTER 8 Decisions, Decisions, Decisions (Selection Structure) . . . . . .115 Someone Might Need to Make a Decision . . . . . . . . . . . . . . . . . . . . . . 116 Going Beyond Rob’s Problems . . . . . . . . . . . . . . . . . . . . . . . . . . 120 The If…Then…Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.