STARTING OUT WITH Visual Basic

Transcription

STARTING OUT WITHVisual Basic Eighth EditionA01 GADD4658 08 SE FM.indd 104/01/19 7:22 PM

A01 GADD4658 08 SE FM.indd 204/01/19 7:22 PM

STARTING OUT WITHVisual Basic Eighth EditionTony GaddisHaywood Community CollegeKip IrvineFlorida International UniversityA01 GADD4658 08 SE FM.indd 304/01/19 7:22 PM

Senior Vice President Courseware Portfolio Management:Marcia HortonVice President, Portfolio Management: Engineering,Computer Science & Global Editions: Julian PartridgeExecutive Portfolio Manager: Matt GoldsteinPortfolio Management Assistant: Meghan JacobyProduct Marketing Manager: Yvonne VannattaField Marketing Manager: Demetrius HallMarketing Assistant: Jon BryantManaging Producer: Scott DisannoContent Producer: Amanda BrandsManufacturing Buyer, Higher Ed, Lake SideCommunications, Inc. (LSC): Maura Zaldivar-GarciaCover Designer: Pearson CSCManager, Rights and Permissions: Ben FerriniCover Image: matin/shutterstockInventory Manager: Bruce BoundyFull-Service Project Manager: Abhishan Sharma,Integra Software Services Pvt. Ltd.Composition: Integra Software Services Pvt. Ltd.Printer/Binder: Lake Side Communications, Inc.Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on theCredits page at the end of the text.Copyright 2020, 2017, 2014, 2011 Pearson Education, Inc., Hoboken, NJ 07030. All rights reserved. Printed in the UnitedStates of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior toany prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical,photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts withinthe Pearson Education Global Rights & Permissions department, please visit www.pearsoned.com/permissions/.Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where thosedesignations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed ininitial caps or all caps.The programs and applications presented in this book have been included for their instructional value. They have been testedwith care, but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations,nor does it accept any liabilities with respect to the programs or applications.microsoft and/or its respective suppliers make no representations about the suitability of the informationcontained in the documents and related graphics published as part of the services for any purpose. all suchdocuments and related graphics are provided “as is” without warranty of any kind. microsoft and/or itsrespective suppliers hereby disclaim all warranties and conditions with regard to this information, includingall warranties and conditions of merchantability. whether express, implied or statutory, fitness for aparticular. purpose, title and non-infringement. in no event shall microsoft and/or its respective suppliers beliable for any special, indirect or c onsequential damages or any damages whatsoever resulting from loss ofuse, data or profits, whether in an action of contract. negligence or other tortious action, arising out of orin c onnection with the use or performance of information available from the services.the documents and related graphics contained herein could include technical inaccuracies or typographicalerrors changes are periodically added to the information herein. microsoft and/or its respective suppliers maymake improvements and/or changes in the product(s) and/or the program(s) described herein at any time partialscreen shots may be viewed in full within the software version specified.microsoft windows , and microsoft office are registered trademarks of the microsoft corporation in theu.s.a and other countries. this book is not sponsored or endorsed by or affiliated with the microsoftcorporation.Library of Congress Cataloging-in-Publication DataNames: Gaddis, Tony, author. Irvine, Kip R., author.Title: Starting out with visual basic / Tony Gaddis, Haywood CommunityCollege, Kip Irvine, Florida International University.Description: Eighth edition. Hoboken : Pearson, [2020] Includesbibliographical references and index.Identifiers: LCCN 2018048219 ISBN 9780135204658 ISBN 0135204658Subjects: LCSH: Visual Basic (Computer program language)Classification: LCC QA76.73.M53 G33 2019 DDC 005.26/8—dc23 LC record available athttps://lccn.loc.gov/201804821910 9 8 7 6 5 4 3 2 1ISBN 10:     0-13-520465-8ISBN 13: 978-0-13-520465-8A01 GADD4658 08 SE FM.indd 404/01/19 7:22 PM

Contents in BriefPrefaceChapter 1Introduction to Programming and Visual BasicChapter 2Creating Applications with Visual BasicChapter 3Variables and Calculations125Chapter 4Making Decisions233Chapter 5Lists and Loops309Chapter 6Procedures and Functions379Chapter 7Multiple Forms, Modules, and Menus429Chapter 8Arrays and More499Chapter 9Files, Printing, and Structure573Chapter 10Working with Databases631Chapter 11Developing Web Applications711Chapter 12Classes, Collections, and Inheritance767Appendix AAdvanced User Interface Controls and Techniques829Appendix BWindows Presentation Foundation (WPF)843Appendix CConverting Mathematical Expressionsto Programming Statements851xviiAppendix D Answers to Checkpoint141853871Appendix EGlossaryAppendix FFunction and Method ReferenceF-1Appendix G Binary and Random-Access FilesG-1Index883Credits909vA01 GADD4658 08 SE FM.indd 504/01/19 7:22 PM

A01 GADD4658 08 SE FM.indd 604/01/19 7:22 PM

ContentsPrefaceChapter 1Introduction to Programming and Visual Basic 1xviiComputer Systems: Hardware and Software . 1Programs and Programming Languages . 4TUTORIAL 1-1: Running the Wage Calculator application . 8TUTORIAL 1-2: Running an application that demonstrates event handlers . 101.3 More about Controls and Programming . 11TUTORIAL 1-3: Running an application that demonstrates various controls . 121.4 The Programming Process . 161.5 Visual Studio . 20TUTORIAL 1-4: Starting Visual Studio and setting up the environment . 20TUTORIAL 1-5: Starting a new Visual Basic project . 24TUTORIAL 1-6: Becoming familiar with Visual Studio . 33Summary 35 Key Terms 36 Review Questions and Exercises 36 Programming Challenges 401.11.2Chapter 2Creating Applications with Visual Basic 412.1 Getting Started with Forms and Controls . 412.2 Creating the GUI for Your First Visual Basic Application: The Hello WorldApplication . 52TUTORIAL 2-1: Creating the GUI for the Hello World application . 532.3 Writing the Code for the Hello World Application . 59TUTORIAL 2-2: Writing code for the Hello World application . 622.4 More About Label Controls . 682.5 Creating Multiple Event Handlers . 76TUTORIAL 2-3: Creating the Language Translator application . 772.6 Making Sense of IntelliSense . 802.7 PictureBox Controls . 81TUTORIAL 2-4: Creating the Guess the President application . 84TUTORIAL 2-5: Writing Click event handlers for PictureBox controls . 872.8 The Visible Property . 90TUTORIAL 2-6: Creating the Card Flip application . 912.9 Writing the Code to Close an Application’s Form . 94TUTORIAL 2-7: Adding an Exit button to the Guess the President application . 952.10 Comments, Blank Lines, and Indentation . 982.11 Dealing with Errors . 992.12 Displaying User Messages at Runtime . 102TUTORIAL 2-8: Displaying Message Boxes . 103viiA01 GADD4658 08 SE FM.indd 704/01/19 7:22 PM

viiiContents2.13 Customizing an Application’s Form . 107TUTORIAL 2-9: Setting the FormBorderStyle property and lockingthe controls on a form . 1092.14 Using Visual Studio Help . 110Summary 112 Key Terms 114 Review Questions 115 Programming Challenges 120Chapter 3 Variables and Calculations 125Gathering Text Input . 125TUTORIAL 3-1: Using a TextBox control . 126TUTORIAL 3-2: Building the Date String application . 1283.2 Variables and Data Types . 131TUTORIAL 3-3: Assigning text to a variable . 1383.3 Performing Calculations . 1423.4 Mixing Different Data Types . 148TUTORIAL 3-4: Creating the Tip Calculator application . 1553.5 Formatting Numbers and Dates . 160TUTORIAL 3-5: Creating the Sale Price Calculator Applicationwith Currency Formatting . 1633.6 Class-Level Variables . 167TUTORIAL 3-6: Creating the Change Counter application . 1683.7 Exception Handling . 173TUTORIAL 3-7: Exception Demonstration . 173TUTORIAL 3-8: Salary Calculator project with exception handling . 1773.8 More GUI Details . 1843.9 The Load Event . 192TUTORIAL 3-9: Getting the system date in the Load event handler . 1933.10 Focus on Program Design and Problem Solving: Buildingthe Room Charge Calculator Application . 195TUTORIAL 3-10: Beginning the Room Charge Calculator application . 200TUTORIAL 3-11: Changing a label’s colors . 2063.11 More About Debugging: Locating Logic Errors . 208TUTORIAL 3-12: Single-stepping through an application’s code at runtime . 209Summary 214 Key Terms 217 Review Questions and Exercises 218 Programming Challenges 2243.1Chapter 4Making Decisions 2334.1 The Decision Structure . 2334.2 The If.Then Statement . 235TUTORIAL 4-1: Examining an application that uses the If.Then statement . 2384.3 The If.Then.Else Statement . 241TUTORIAL 4-2: Completing an application that uses theIf.Then.Else statement . 2424.4 The If.Then.ElseIf Statement . 244TUTORIAL 4-3: Completing an application that uses theIf.Then.ElseIf statement . 2454.5 Nested If Statements . 251A01 GADD4658 08 SE FM.indd 804/01/19 7:22 PM

ContentsixTUTORIAL 4-4: Completing an application with a nested If statement . 2514.6 Logical Operators . 2534.7 Comparing, Testing, and Working with Strings . 259TUTORIAL 4-5: Examining an application that performs string comparisons . 262TUTORIAL 4-6: Completing a string searching application . 2674.8 The Select Case Statement . 270TUTORIAL 4-7: Examining Crazy Al’s Sales Commission Calculatorapplication . 2734.9 Introduction to Input Validation . 276TUTORIAL 4-8: Examining an application that uses TryParse forinput validation . 2784.10 Focus on GUI Design: Radio Buttons and Check Boxes . 281TUTORIAL 4-9: Completing an application with radio buttonsand check boxes . 2834.11 Focus on Program Design and Problem Solving: Buildingthe Health Club Membership Fee Calculator Application . 284TUTORIAL 4-10: Building the Health Club Membership FeeCalculator application . 290Summary 294 Key Terms 295 Review Questions and Exercises 296 Programming Challenges 301Chapter 5Lists and Loops 3095.1 Input Boxes . 3095.2 List Boxes . 311TUTORIAL 5-1: Creating list boxes . 3165.3 Introduction to Loops: The Do While Loop . 320TUTORIAL 5-2: Completing an application that uses the Do While loop . 321TUTORIAL 5-3: Modifying the Do While Demo application to usea posttest loop . 325TUTORIAL 5-4: Using a loop to keep a running total . 327TUTORIAL 5-5: Examining an application that uses a user-controlled loop . 3295.4 The Do Until and For.Next Loops . 331TUTORIAL 5-6: Examining an application that uses the Do Until loop . 332TUTORIAL 5-7: Examining an application that uses the For.Next loop . 336TUTORIAL 5-8: Completing an application that uses the For.Next loop . 3375.5 Nested Loops . 3415.6 Multicolumn List Boxes, Checked List Boxes, and Combo Boxes . 342TUTORIAL 5-9: Creating combo boxes . 3465.7 Random Numbers . 348TUTORIAL 5-10: Creating the Coin Toss application . 3505.8 Simplifying Code using the With.End With Statement . 3525.9 ToolTips . 353TUTORIAL 5-11: Adding ToolTips to an application . 3545.10 Focus on Program Design and Problem Solving: Building the Vehicle LoanCalculator Application . 355TUTORIAL 5-12: Building the Vehicle Loan Calculator application . 358Summary 363 Key Terms 364 Review Questions and Exercises 364 Programming Challenges 370A01 GADD4658 08 SE FM.indd 904/01/19 7:22 PM

xContentsChapter 6Procedures and Functions 379Procedures . 380TUTORIAL 6-1: Examining an application with a procedure . 380TUTORIAL 6-2: Creating and calling procedures . 3836.2 Passing Arguments to Procedures . 387TUTORIAL 6-3: Examining an application that demonstrates passingan argument to a procedure . 389TUTORIAL 6-4: Working with ByVal and ByRef . 3926.3 Functions . 395TUTORIAL 6-5: The Sale Price Calculator application . 3966.4 More about Debugging: Stepping Into, Over, and Out ofProcedures and Functions . 403TUTORIAL 6-6: Practicing the Step Into command . 404TUTORIAL 6-7: Practicing the Step Over command . 405TUTORIAL 6-8: Practicing the Step Out command . 4056.5 Focus on Program Design and Problem Solving: Building theBagel and Coffee Price Calculator Application . 407TUTORIAL 6-9: Building the Bagel House application . 412Summary 416 Key Terms 416 Review Questions and Exercises 417 Programming Challenges 4206.1Chapter 7Multiple Forms, Modules, and Menus 429Multiple Forms . 429application with two forms . 437TUTORIAL 7-2: Completing an application that displays modaland modeless forms . 440TUTORIAL 7-3: Accessing a control on a different form . 4467.2 Modules . 451TUTORIAL 7-4: Examining an application that uses a module . 4547.3 Menus . . 461TUTORIAL 7-5: Building a menu . 4687.4 Focus on Problem Solving: Building the High Adventure TravelAgency Price Quote Application . 473TUTORIAL 7-6: Building the High Adventure Travel AgencyPrice Quote application . 475Summary 481 Key Terms 482 Review Questions and Exercises 482 Programming Challenges 4877.1TUTORIAL 7-1: Creating anChapter 8Arrays and More 499Arrays . 499TUTORIAL 8-1: Using an array to hold a list of random lottery numbers . 504TUTORIAL 8-2: Using an array to hold a list of names entered by the user . 507TUTORIAL 8-3: Completing an application that uses arrayelements in a calculation . 5108.2 Array Processing Techniques . 514TUTORIAL 8-4: Using parallel arrays . 5188.1A01 GADD4658 08 SE FM.indd 1004/01/19 7:22 PM

Contentsxi8.3 Procedures and Functions That Work with Arrays . 524TUTORIAL 8-5: Examining an application that passes an arrayto procedures and functions . 5268.4 Multidimensional Arrays . 531TUTORIAL 8-6: Completing the Seating Chart application . 5358.5 Focus on GUI Design: The Enabled Property and theTimer Control . 540TUTORIAL 8-7: The Timer Demo . 541TUTORIAL 8-8: Creating the Catch Me game . 5428.6 Focus on GUI Design: Anchoring and Docking Controls . 5458.7 Focus on Problem Solving: Building the Demetris Leadership CenterApplication . 548TUTORIAL 8-9: Building the Demetris Leadership CenterSales Reporting application . 5518.8 Using Lists to Hold Information (Optional Topic) . 554TUTORIAL 8-10: Building a List from user input . 556Summary 559 Key Terms 560 Review Questions and Exercises 561 Programming Challenges 566Chapter 9Files, Printing, and Structures573Using Files . 573TUTORIAL 9-1: Completing an application that writes data to a file . 578TUTORIAL 9-2: Completing an application that reads a file . 583TUTORIAL 9-3: Examining an application that detects the end of a file . 5879.2 The OpenFileDialog, SaveFileDialog, FontDialog,and ColorDialog Controls . 593TUTORIAL 9-4: Creating a Simple Text Editor application . 5979.3 The PrintDocument Control . 606TUTORIAL 9-5: Adding printing capabilities to theSimple Text Editor application . 6089.4 Structures . 613TUTORIAL 9-6: Examining an application with a structure . 616Summary 618 Key Terms 618 Review Questions and Exercises 619 Programming Challenges 6259.1Chapter 10 Working with Databases 63110.1 Database Management Systems . 63110.2 Database Concepts . 63210.3 DataGridView Control . 636TUTORIAL 10-1: Showing a database table in a DataGridView control . 637TUTORIAL 10-2: Sorting and updating the SalesStaff table . 64510.4 Data-Bound Controls . 648TUTORIAL 10-3: Binding a DataGridView to the SalesStaff table . 653TUTORIAL 10-4: Binding individual controls to the SalesStaff table . 655TUTORIAL 10-5: Displaying the Karate Members table in aListBox Control . 659A01 GADD4658 08 SE FM.indd 1104/01/19 7:22 PM

xiiContentsTUTORIAL 10-6: Inserting Karate memberpayments . 662a total to the Insert KaratePayments application . 66610.5 Structured Query Language (SQL) . 667TUTORIAL 10-8: Filtering rows in the SalesStaff table . 67510.6 Focus on Problem Solving: Karate School ManagementApplication . 677TUTORIAL 10-9: Creating the Karate School Manager startup form . 680TUTORIAL 10-10: Adding the Membership / List All functionto the Karate School Manager . 681TUTORIAL 10-11: Adding the Membership / Add New Memberfunction to the Karate School Manager . 684TUTORIAL 10-12: Adding the Membership / Find Memberfunction to the Karate School Manager . 686TUTORIAL 10-13: Adding the Payments / All Members functionto the Karate School Manager . 68810.7 Introduction to LINQ . 69510.8 Creating Your Own Database . 696TUTORIAL 10-14: Creating the Movie database and the Films table . 697Summary 701 Key Terms 702 Review Questions and Exercises 703 Programming Challenges 706TUTORIAL 10-7: AddingChapter 11 Developing Web Applications71111.1 Programming for the Web . 71111.2 Creating ASP.NET Applications . 715TUTORIAL 11-1: Creating the Click application . 71911.3 Web Server Controls . 724TUTORIAL 11-2: Student Picnic application . 72611.4 Designing Web Forms . 734TUTORIAL 11-3: Signing up for a Kayak Tour . 73711.5 Applications with Multiple Web Pages . 741TUTORIAL 11-4: Adding a description form to the Kayak Tour application . 74211.6 Using Databases . 743TUTORIAL 11-5: Displaying the Karate Members table in a GridView . 747TUTORIAL 11-6: Updating the Karate Members table . 752Summary 760 Key Terms 761 Review Questions and Exercises 762 Programming Challenges 765Chapter 12 Classes, Collections, and Inheritance 76712.1 Classes and Objects . 76712.2 Creating a Class . 770TUTORIAL 12-1: Creating the Student Data application . 78412.3 Collections . 78812.4 Focus on Problem Solving: Creating theStudent Collection Application . 794TUTORIAL 12-2: Completing the Student Collection application . 794A01 GADD4658 08 SE FM.indd 1204/01/19 7:22 PM

Contentsxiii12.5 The Object Browser . 799TUTORIAL 12-3: Using the Object Browser . 79912.6 Introduction to Inheritance . 801TUTORIAL 12-4: Completing an application that uses inheritance . 808Summary 815 Key Terms 816 Review Questions and Exercises 817 Programming Challenges 824Appendix A Advanced User Interface Controls and Techniques . 829Appendix B Windows Presentation Foundation (WPF) . 843Appendix C Converting Mathematical Expressions toProgramming Statements

Contents in Brief Preface xvii Chapter 1 Introduction to Programming and Visual Basic 1 Chapter 2 Creating Applications with Visual Basic 41 Chapter 3 Variables and Calculations 125 Chapter 4 Making Decisions 233 Chapter 5 Lists and Loops 309 Chapter 6 Procedures and Functions 379 Chapter 7 Multiple Fo