This page intentionally left blank
STARTING OUT WITH
Visual Basic® 2012 Sixth Edit ion
This page intentionally left blank
STARTING OUT WITH
Visual Basic® 2012 Sixth Edit ion
Tony Gaddis Haywood Community College
Kip Irvine Florida International University
Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montréal Toronto
Delhi Mexico City Sa~o Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Editorial Director, ECS: Marcia Horton Acquisitions Editor: Matt Goldstein Editorial Assistant: Jenah Blitz-Stoehr Director of Marketing: Christy Lesko Marketing Manager: Yezan Alayan Senior Marketing Coordinator: Kathryn Ferranti Director of Production: Erin Gregg Senior Managing Editor: Scott Disanno Senior Production Project Manager: Marilyn Lloyd Manufacturing Buyer: Lisa McDowell Art Director: Anthony Gemmellaro Cover Designer: Joyce Cosentino Wells Manager, Rights and Permissions: Michael Joyce Cover Image: © Vladimir Nenov/Alamy Media Project Manager: Renata Butera Full-Service Project Manager: Jogender Taneja/iEnergizer Aptara®, Inc. Composition: Aptara®, Inc. Printer/Binder: Edwards Brothers, Inc. Cover Printer: Lehigh-Phoenix Color
Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on the Credits page at the end of the text.
Copyright © 2014, 2011, 2010 Pearson Education, Inc., publishing as Addison-Wesley. All rights reserved. Printed in the United States of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to 201-236-3290.
Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps.
The programs and applications presented in this book have been included for their instructional value. They have been tested with 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.
Library of Congress Cataloging-in-Publication Data available upon request.
10 9 8 7 6 5 4 3 2 1
ISBN 10: 0-13-312808-3 ISBN 13: 978-0-13-312808-6
v
Contents in Brief
Preface xvii
Chapter 1 Introduction to Programming and Visual Basic 1
Chapter 2 Creating Applications with Visual Basic 43
Chapter 3 Variables and Calculations 111
Chapter 4 Making Decisions 209
Chapter 5 Lists and Loops 285
Chapter 6 Procedures and Functions 355
Chapter 7 Multiple Forms, Modules, and Menus 403
Chapter 8 Arrays and More 469
Chapter 9 Files, Printing, and Structures 541
Chapter 10 Working with Databases 597
Chapter 11 Developing Web Applications 677
Chapter 12 Classes, Collections, and Inheritance 733
Appendix A Advanced User Interface Controls and Techniques 795
Appendix B Windows Presentation Foundation (WPF) 809
Appendix C Converting Mathematical Expressions to Programming Statements 821
Appendix D Answers to Checkpoints 823
Appendix E Glossary 841
Index 853
Credits 883
This page intentionally left blank
vii
Contents
Preface xvii
Chapter 1 Introduction to Programming and Visual Basic 1
1.1 Computer Systems: Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Programs and Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 TUTORIAL 1-1: Running the Wage Calculator application . . . . . . . . . . . . . . . . . . . . . 8 TUTORIAL 1-2: Running an application that demonstrates event handlers . . . . . . . . 11 1.3 More about Controls and Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 TUTORIAL 1-3: Running an application that demonstrates various controls . . . . . . . 12 1.4 The Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5 Visual Studio and Visual Studio Express (the Visual Basic Environment) . . . . 20 TUTORIAL 1-4: Starting Visual Studio and setting up the environment . . . . . . . . . . . 21 TUTORIAL 1-5: Starting a new Visual Basic project . . . . . . . . . . . . . . . . . . . . . . . . . . 25 TUTORIAL 1-6: Becoming familiar with the Visual Studio environment . . . . . . . . . . 34
Summary 36 • Key Terms 37 • Review Questions and Exercises 37 • Programming Challenges 41
Chapter 2 Creating Applications with Visual Basic 43
2.1 Focus on Problem Solving: Building the Directions Application . . . . . . . . . . 43 TUTORIAL 2-1: Beginning the Directions application . . . . . . . . . . . . . . . . . . . . . . . . 46 TUTORIAL 2-2: Adding a Label control to the Directions application . . . . . . . . . . . . 48 TUTORIAL 2-3: Changing the Label’s font size and style . . . . . . . . . . . . . . . . . . . . . . 50 TUTORIAL 2-4: Deleting a control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 TUTORIAL 2-5: Inserting a PictureBox control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 TUTORIAL 2-6: Running the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 TUTORIAL 2-7: Opening an existing project and becoming familiar
with the Properties window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 2.2 Focus on Problem Solving: Responding to Events . . . . . . . . . . . . . . . . . . . . 63 TUTORIAL 2-8: Adding a Label control for the written directions . . . . . . . . . . . . . . . 64 TUTORIAL 2-9: Adding the Display Directions button and its Click
event handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 TUTORIAL 2-10: Adding the Exit button and its Click event handler . . . . . . . . . . . . 76 TUTORIAL 2-11: Adding comments to the Directions project code . . . . . . . . . . . . . . 79 TUTORIAL 2-12: Changing the text colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 TUTORIAL 2-13: Setting the FormBorderStyle property and locking
the controls in the Directions application . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.3 Modifying a Control’s Text Property with Code . . . . . . . . . . . . . . . . . . . . . 85 TUTORIAL 2-14: Examining an application that displays messages
in a Label control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 2.4 The AutoSize, BorderStyle, and TextAlign Properties . . . . . . . . . . . . . . . . . . 88 2.5 Displaying User Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
viii Contents
TUTORIAL 2-15: Displaying message boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.6 Clickable Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 TUTORIAL 2-16: Writing Click event handlers for PictureBox controls . . . . . . . . . . 95 2.7 Using Visual Studio Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 2.8 Debugging Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 TUTORIAL 2-17: Locating a compile error in design mode . . . . . . . . . . . . . . . . . . . 100
Summary 102 • Key Terms 103 • Review Questions and Exercises 104 • Programming Challenges 106
Chapter 3 Variables and Calculations 111
3.1 Gathering Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 TUTORIAL 3-1: Using a TextBox control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 TUTORIAL 3-2: Building the Date String application . . . . . . . . . . . . . . . . . . . . . . . 115 TUTORIAL 3-3: Using the Focus method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 TUTORIAL 3-4: Changing the tab order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 TUTORIAL 3-5: Setting access keys, accept, and cancel buttons . . . . . . . . . . . . . . . 123 3.2 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 TUTORIAL 3-6: Assigning text to a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 3.3 Performing Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 3.4 Mixing Different Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 TUTORIAL 3-7: Examining a Simple Calculator application . . . . . . . . . . . . . . . . . . 151 3.5 Formatting Numbers and Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 TUTORIAL 3-8: Examining the Format Demo application . . . . . . . . . . . . . . . . . . . 157 3.6 Class-Level Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 3.7 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 TUTORIAL 3-9: Exception Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 TUTORIAL 3-10: Salary Calculator project with exception handling . . . . . . . . . . . 164 3.8 Group Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 3.9 The Load Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 3.10 Focus on Program Design and Problem Solving: Building the Room
Charge Calculator Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 TUTORIAL 3-11: Beginning the Room Charge Calculator application . . . . . . . . . . 179 TUTORIAL 3-12: Changing a label’s colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 3.11 More about Debugging: Locating Logic Errors . . . . . . . . . . . . . . . . . . . . . 186 TUTORIAL 3-13: Single-stepping through an application’s code at runtime . . . . . . 186
Summary 192 • Key Terms 195 • Review Questions and Exercises 196 • Programming Challenges 202
Chapter 4 Making Decisions 209
4.1 The Decision Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 4.2 The If...Then Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 TUTORIAL 4-1: Examining an application that uses the If...Then statement . . 214 4.3 The If...Then...Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 TUTORIAL 4-2: Completing an application that uses
the If...Then...Else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 4.4 The If...Then...ElseIf Statement . . . . . . . . . . . . . . . . . . . . . . . . . 220
Contents ix
TUTORIAL 4-3: Completing an application that uses the If...Then...ElseIf statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.5 Nested If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 TUTORIAL 4-4: Completing an application with a nested If statement . . . . . . . . . 227 4.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 4.7 Comparing, Testing, and Working with Strings . . . . . . . . . . . . . . . . . . . . 235 TUTORIAL 4-5: Examining an application that performs string comparisons . . . . . 238 TUTORIAL 4-6: Completing a string searching application . . . . . . . . . . . . . . . . . . . 243 4.8 The Select Case Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 TUTORIAL 4-7: Examining Crazy Al’s Sales Commission
Calculator application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 4.9 Introduction to Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 TUTORIAL 4-8: Examining an application that uses TryParse
for input validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 4.10 Focus on GUI Design: Radio Buttons and Check Boxes . . . . . . . . . . . . . . . 257 TUTORIAL 4-9: Completing an application with radio buttons
and check boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 4.11 Focus on Program Design and Problem Solving: Building
the Health Club Membership Fee Calculator Application . . . . . . . . . . . . . . . 260 TUTORIAL 4-10: Building the Health Club Membership Fee Calculator
application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Summary 270 • Key Terms 271 • Review Questions and Exercises 272 • Programming Challenges 277
Chapter 5 Lists and Loops 285
5.1 Input Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 5.2 List Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 TUTORIAL 5-1: Creating list boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 5.3 Introduction to Loops: The Do While Loop . . . . . . . . . . . . . . . . . . . . . . 296 TUTORIAL 5-2: Completing an application that uses the Do While loop . . . . . . . 297 TUTORIAL 5-3: Modifying the Do While Demo application to use
a posttest loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 TUTORIAL 5-4: Using a loop to keep a running total . . . . . . . . . . . . . . . . . . . . . . . 303 TUTORIAL 5-5: Examining an application that uses a user-controlled loop . . . . . . . 305 5.4 The Do Until and For...Next Loops . . . . . . . . . . . . . . . . . . . . . . . . . 307 TUTORIAL 5-6: Examining an application that uses the Do Until loop . . . . . . . . 308 TUTORIAL 5-7: Examining an application that uses the For...Next loop . . . . . . 312 TUTORIAL 5-8: Completing an application that uses the For...Next loop . . . . . 313 5.5 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 5.6 Multicolumn List Boxes, Checked List Boxes, and Combo Boxes . . . . . . . . 318 TUTORIAL 5-9: Creating combo boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 5.7 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 TUTORIAL 5-10: Creating the Coin Toss application . . . . . . . . . . . . . . . . . . . . . . . . 326 5.8 Simplifying Code with the With...End With Statement . . . . . . . . . . . 328 5.9 ToolTips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 TUTORIAL 5-11: Adding ToolTips to an application . . . . . . . . . . . . . . . . . . . . . . . . 330 5.10 Focus on Program Design and Problem Solving: Building
the Vehicle Loan Calculator Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
x Contents
TUTORIAL 5-12: Building the Vehicle Loan Calculator application . . . . . . . . . . . . . 334
Summary 339 • Key Terms 340 • Review Questions and Exercises 340 • Programming Challenges 346
Chapter 6 Procedures and Functions 355
6.1 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 TUTORIAL 6-1: Examining an application with a procedure . . . . . . . . . . . . . . . . . . 356 TUTORIAL 6-2: Creating and calling procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 6.2 Passing Arguments to Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 TUTORIAL 6-3: Examining an application that demonstrates passing
an argument to a procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 TUTORIAL 6-4: Working with ByVal and ByRef . . . . . . . . . . . . . . . . . . . . . . . . . 368 6.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 TUTORIAL 6-5: Sale Price Calculator application . . . . . . . . . . . . . . . . . . . . . . . . . . 372 6.4 More about Debugging: Stepping Into, Over,
and Out of Procedures and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 TUTORIAL 6-6: Practicing the Step Into command . . . . . . . . . . . . . . . . . . . . . . . . . 380 TUTORIAL 6-7: Practicing the Step Over command . . . . . . . . . . . . . . . . . . . . . . . . 381 TUTORIAL 6-8: Practicing the Step Out command . . . . . . . . . . . . . . . . . . . . . . . . . 381 6.5 Focus on Program Design and Problem Solving: Building
the Bagel and Coffee Price Calculator Application . . . . . . . . . . . . . . . . . . . . 383 TUTORIAL 6-9: Building the Bagel House application . . . . . . . . . . . . . . . . . . . . . . . 388
Summary 392 • Key Terms 392 • Review Questions and Exercises 393 • Programming Challenges 396
Chapter 7 Multiple Forms, Modules, and Menus 403
7.1 Multiple Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 TUTORIAL 7-1: Creating an application with two forms . . . . . . . . . . . . . . . . . . . . . 411 TUTORIAL 7-2: Completing an application that displays modal
and modeless forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 TUTORIAL 7-3: Accessing a control on a different form . . . . . . . . . . . . . . . . . . . . . 420 7.2 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 TUTORIAL 7-4: Examining an application that uses a module . . . . . . . . . . . . . . . . 428 7.3 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 TUTORIAL 7-5: Building a menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 7.4 Focus on Problem Solving: Building the High Adventure Travel
Agency Price Quote Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 TUTORIAL 7-6: Building the High Adventure Travel Agency Price Quote
application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Summary 455 • Key Terms 456 • Review Questions and Exercises 456 • Programming Challenges 461
Chapter 8 Arrays and More 469
8.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 TUTORIAL 8-1: Using an array to hold a list of random lottery numbers . . . . . . . . 474 TUTORIAL 8-2: Using an array to hold a list of names entered by the user . . . . . . . 477
Contents xi
TUTORIAL 8-3: Completing an application that uses array elements in a calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
8.2 Array Processing Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 TUTORIAL 8-4: Using parallel arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 8.3 Procedures and Functions That Work with Arrays . . . . . . . . . . . . . . . . . . . 494 TUTORIAL 8-5: Examining an application that passes an array
to procedures and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 8.4 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 TUTORIAL 8-6: Completing the Seating Chart application . . . . . . . . . . . . . . . . . . . 505 8.5 Focus on GUI Design: The Enabled Property and the Timer Control . . . . .510 TUTORIAL 8-7: The Timer Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 TUTORIAL 8-8: Creating the Catch Me game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 8.6 Focus on GUI Design: Anchoring and Docking Controls . . . . . . . . . . . . . . 515 8.7 Focus on Problem Solving: Building the Demetris Leadership
Center Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 TUTORIAL 8-9: Building the Demetris Leadership Center Sales
Reporting application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 8.8 Using Lists to Hold Information (Optional Topic) . . . . . . . . . . . . . . . . . . . 524 TUTORIAL 8-10: Building a List from User Input . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Summary 529 • Key Terms 530 • Review Questions and Exercises 531 • Programming Challenges 536
Chapter 9 Files, Printing, and Structures 541
9.1 Using Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 TUTORIAL 9-1: Completing an application that writes data to a file . . . . . . . . . . . . 546 TUTORIAL 9-2: Completing an application that reads a file . . . . . . . . . . . . . . . . . . 551 TUTORIAL 9-3: Examining an application that detects the end of a file . . . . . . . . . 555 9.2 The OpenFileDialog, SaveFileDialog, FontDialog, and ColorDialog
Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .561 TUTORIAL 9-4: Creating a Simple Text Editor application . . . . . . . . . . . . . . . . . . . 565 9.3 The PrintDocument Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 TUTORIAL 9-5: Adding printing capabilities to the Simple Text Editor
application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 9.4 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 TUTORIAL 9-6: Examining an application with a structure . . . . . . . . . . . . . . . . . . . 583
Summary 585 • Key Terms 585 • Review Questions and Exercises 586 • Programming Challenges 592
Chapter 10 Working with Databases 597
10.1 Database Management Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 10.2 Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598 10.3 DataGridView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 TUTORIAL 10-1: Showing a database table in a DataGridView control . . . . . . . . . . 603 TUTORIAL 10-2: Sorting and updating the SalesStaff table . . . . . . . . . . . . . . . . . . . 611 10.4 Data-Bound Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614 TUTORIAL 10-3: Binding a DataGridView to the SalesStaff table . . . . . . . . . . . . . . 619 TUTORIAL 10-4: Binding individual controls to the SalesStaff table . . . . . . . . . . . . 621
xii Contents
TUTORIAL 10-5: Displaying the Karate Members table in a ListBox Control . . . . . 625 TUTORIAL 10-6: Inserting Karate member payments . . . . . . . . . . . . . . . . . . . . . . . 628 TUTORIAL 10-7: Adding a total to the Insert Karate Payments application . . . . . . 632 10.5 Structured Query Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633 TUTORIAL 10-8: Filtering rows in the SalesStaff table . . . . . . . . . . . . . . . . . . . . . . . 641 10.6 Focus on Problem Solving: Karate School Management Application . . . . . . 643 TUTORIAL 10-9: Creating the Karate School Manager startup form . . . . . . . . . . . . 647 TUTORIAL 10-10: Adding the Membership / List All function
to the Karate School Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 TUTORIAL 10-11: Adding the Membership / Add New Member function
to the Karate School Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 TUTORIAL 10-12: Adding the Membership / Find Member function
to the Karate School Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 TUTORIAL 10-13: Adding the Payments / All Members function
to the Karate School Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654 10.7 Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 10.8 Creating Your Own Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .662 TUTORIAL 10-14: Creating the Movie database and the Films table . . . . . . . . . . . 663
Summary 667 • Key Terms 668 • Review Questions and Exercises 669 • Programming Challenges 671
Chapter 11 Developing Web Applications 677
11.1 Programming for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 11.2 Creating ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 TUTORIAL 11-1: Creating the Click application . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 11.3 Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690 TUTORIAL 11-2: Student Picnic application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 11.4 Designing Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 TUTORIAL 11-3: Signing up for a Kayak Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703 11.5 Applications with Multiple Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 TUTORIAL 11-4: Adding a description form to the Kayak Tour application . . . . . . 709 11.6 Using Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710 TUTORIAL 11-5: Displaying the Karate Members table in a GridView . . . . . . . . . . 713 TUTORIAL 11-6: Updating the Karate Members table . . . . . . . . . . . . . . . . . . . . . . . 718
Summary 726 • Key Terms 727 • Review Questions and Exercises 728 • Programming Challenges 731
Chapter 12 Classes, Collections, and Inheritance 733
12.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 12.2 Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736 TUTORIAL 12-1: Creating the Student Data application . . . . . . . . . . . . . . . . . . . . . 749 12.3 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753 12.4 Focus on Problem Solving: Creating the Student Collection Application . . . 760 TUTORIAL 12-2: Completing the Student Collection application . . . . . . . . . . . . . . 760 12.5 The Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765 TUTORIAL 12-3: Using the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765 12.6 Introduction to Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
Contents xiii
TUTORIAL 12-4: Completing an application that uses inheritance . . . . . . . . . . . . . 774
Summary 781 • Key Terms 782 • Review Questions and Exercises 783 • Programming Challenges 790
Appendix A Advanced User Interface Controls and Techniques . . . . . . 795
Appendix B Windows Presentation Foundation (WPF) . . . . . . . . . . . . . . 809
Appendix C Converting Mathematical Expressions to Programming Statements . . . . . . . . . . . . . . . . . . . . . . . . . 821
Appendix D Answers to Checkpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Appendix E Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
VideoNoteLOCATION OF VIDEONOTES IN THE TEXT
Chapter 1 Forms, Controls, and Properties, p. 9
Event-Driven Programming, p. 10
Tutorial 1-4 Walkthrough, p. 21
Tutorial 1-5 Walkthrough, p. 25
Tutorial 1-6 Walkthrough, p. 34
Solving the Sales Tax Problem, p. 41
Chapter 2 Tutorial 2-1 Walkthrough, p. 46
Tutorial 2-2 Walkthrough, p. 48
Tutorial 2-3 Walkthrough, p. 50
Tutorial 2-5 Walkthrough, p. 53
Tutorial 2-6 Walkthrough, p. 57
Responding to Events, p. 63
Tutorial 2-8 Walkthrough, p. 64
Tutorial 2-9 Walkthrough, p. 68
Tutorial 2-10 Walkthrough, p. 76
Using IntelliSense, p. 84
The Name and Address Problem, p. 107
Chapter 3 Tutorial 3-2 Walkthrough, p. 115
Introduction to Variables, p. 124
Problem Solving with Variables, p. 136
Converting TextBox Input, p. 149
Exception Demonstration, p. 161
Tutorial 3-10 Walkthrough, p. 164
Building a Kayak Rental Application, p. 195
The Miles per Gallon Calculator Problem, p. 202
Chapter 4 The If...Then Statement, p. 211
The If...Then...Else Statement, p. 217
Tutorial 4-2 Walkthrough, p. 218
Tutorial 4-3 Walkthrough, p. 221
Tutorial 4-4 Walkthrough, p. 227
Validating Input with TryParse, p. 254
Improving the Kayak Rental Application, p. 272
The Roman Numeral Converter Problem, p. 277
Chapter 5 The Do While Loop, p. 296
Tutorial 5-2 Walkthrough, p. 297
Tutorial 5-3 Walkthrough, p. 301
The For...Next Loop, p. 310
Improving the Kayak Rental Application, p. 340
The Sum of Numbers Problems, p. 346
Chapter 6 Tutorial 6-2 Walkthrough, p. 359
Passing Arguments to Procedures, p. 363
Functions, p. 371
Tutorial 6-5 Walkthrough, p. 372
Tutorial 6-6 Walkthrough, p. 380
Improving the Kayak Rental Application, p. 393
The Retail Price Calculator Problem, p. 396
Chapter 7 Creating and Displaying a Second Form, p. 405
Tutorial 7-1 Walkthrough, p. 411
Creating a Menu, p. 435
Adding menus and forms to the Kayak Rental Application, p. 456
The Astronomy Helper Problem, p. 466
Chapter 8 Accessing Array Elements with a Loop, p. 472
Tutorial 8-2 Walkthrough, p. 477
Tutorial 8-3 Walkthrough, p. 480
Using Arrays to Look Up Information in the Kayak Rental Application, p. 531
The Lottery Application, p. 539
Chapter 9 Writing Data to a File, p. 543
Reading Data from a File, p. 549
Tutorial 9-3 Walkthrough, p. 556
Files and Colors in the Kayak Rental Application, p. 586
The Random Number File Generator Problem, p. 594
Chapter 10 The DataGridView Control, p. 602
Data-Bound Controls, p. 614
Tutorial 10-4 Walkthrough, p. 621
Adding a Database to the Kayak Rental Application, p. 669
The Karate Members Grid Problem, p. 671
The Karate Payments by a Single Member Problem, p. 674
Chapter 11 Creating a Simple Web Application, p. 682
Tutorial 11-2 Walkthrough, p. 692
Building a Movie Tracking Application, p. 728
The Stadium Seating Problem, p. 731
Chapter 12 Creating a Class, p. 737
Collections, p. 753
Tutorial 12-2 Walkthrough, p. 760
Kayak Rentals with Classes, p. 783
The Motor Class Problem, p. 791
This page intentionally left blank
xvii
Preface
Welcome to Starting Out with Visual Basic 2012, Sixth Edition. This book isintended for use in an introductory programming course. It is designed for stu- dents who have no prior programming background, but even experienced students will benefit from its depth of detail and the chapters covering databases, Web applications, and other advanced topics. The book is written in clear, easy-to-understand language and covers all the necessary topics of an introductory programming course. The text is rich in concise, practical, and real-world example programs, so the student not only learns how to use the various controls, constructs, and features of Visual Basic, but also learns why and when to use them.
Changes in the Sixth Edition • The book is completely updated for Visual Basic 2012—This book is designed for
use with Visual Studio 2012 and Visual Studio Express 2012 for Windows Desktop. • Introduction of the StatusStrip Control for Displaying User Messages—The
StatusStrip control is introduced in Chapter 2 as a means for displaying user mes- sages. In many situations, the StatusStrip control is more effective than a message box because it does not interrupt the user’s workflow.
• A New Sequence of VideoNotes has been added throughout the book—Beginning in Chapter 3, a new sequence of video tutorials that promote an accelerated learning experience are introduced. Each set of tutorials uses topics from the current and pre- vious chapters to build an application that integrates visual design and interaction principles. Students who wish to accelerate their learning may use these videos to summarize and apply essential topics in these chapters.
• A New Tutorial for Creating a Database—A new tutorial has been added to Chap- ter 10 showing the student how to create a database within Visual Studio, and add data to the database.
Visual Studio 2012 Express for Windows Desktop Each new copy of this book comes with Microsoft’s Visual Studio 2012 Express for Win- dows Desktop—a streamlined product that captures the best elements of Visual Studio in an ideal format for learning programming. The Express edition offers an impressive set of tools for developing and debugging Visual Basic Windows Forms applications and Web applications, applications, including those that work with databases and use SQL.
A Look at Visual Basic: Past and Present The first version of Visual Basic was introduced in 1991. Prior to its introduction, writing a GUI interface for an application was no small task. Typically, it required hundreds of lines of C code for even the simplest Hello World program. Additionally, an understand- ing of graphics, memory, and complex system calls was often necessary. Visual Basic was revolutionary because it significantly simplified this process. With Visual Basic, a programmer could visually design an application’s user interface. Visual Basic would then generate the code necessary to display and operate the interface. This allowed the programmer to spend less time writing GUI code and more time writing code to perform meaningful tasks.
xviii Preface
The evolution of Visual Basic from version 1 to version 6 followed a natural progression. Each new release was an improved version of the previous release, providing additional features and enhancements. Visual Basic versions offered backward compatibility, where code written in an older version was compatible with a newer version of the Visual Basic development environment.
In 2002, Microsoft released a new object-oriented software platform known as .NET. The .NET platform consists of several layers of software that sit above the operating system and provide a secure, managed environment in which programs can execute. In addition to providing a managed environment for applications to run, .NET also provided new technologies for creating Internet-based programs and programs that provide services over the Web. Along with the introduction of the .NET platform, Microsoft introduced a new version of Visual Basic known as VB .NET 2002, which allowed programmers to write desktop applications or Web applications for the .NET platform.
VB .NET was not merely a new and improved version of VB 6, however. VB .NET was a totally new programming environment, and the Visual Basic language was dramatically revised. The changes were substantial enough that programs written in earlier versions of Visual Basic were not compatible with VB .NET. Microsoft provided a utility that could be used to convert older Visual Basic applications to the new VB .NET syntax, but the results were not always perfect. Although this was frustrating for some Visual Basic developers, Microsoft reasoned the changes were necessary to ensure that Visual Basic continued to evolve as a modern, professional programming environment.