ebook img

Starting Out With Visual Basic 2012 PDF

935 Pages·2013·18.412 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 Starting Out With Visual Basic 2012

This page intentionally left blank STARTING OUT WITH Visual Basic 2012 ® Sixth Edition This page intentionally left blank STARTING OUT WITH Visual Basic 2012 ® Sixth Edition 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 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 v This page intentionally left blank 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 Calculatorapplication . . . . . . . . . . . . . . . . . . . . .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 DirectionsApplication . . . . . . . . . .43 TUTORIAL 2-1: Beginning the Directionsapplication . . . . . . . . . . . . . . . . . . . . . . . .46 TUTORIAL 2-2: Adding a Label control to the Directionsapplication . . . . . . . . . . . .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 Propertieswindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .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 Directionsbutton and its Click event handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 TUTORIAL 2-10: Adding the Exitbutton and its Click event handler . . . . . . . . . . . .76 TUTORIAL 2-11: Adding comments to the Directionsproject code . . . . . . . . . . . . . .79 TUTORIAL 2-12: Changing the text colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 TUTORIAL 2-13: Setting the FormBorderStyle property and locking the controls in the Directionsapplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 vii 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 Stringapplication . . . . . . . . . . . . . . . . . . . . . . . 115 TUTORIAL 3-3: Using the Focusmethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Calculatorapplication . . . . . . . . . . . . . . . . . . 151 3.5 Formatting Numbers and Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 TUTORIAL 3-8: Examining the Format Demoapplication . . . . . . . . . . . . . . . . . . . 157 3.6 Class-Level Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 3.7 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 TUTORIAL 3-9: Exception Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 TUTORIAL 3-10: Salary Calculatorproject 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 CalculatorApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 TUTORIAL 3-11: Beginning the Room Charge Calculatorapplication . . . . . . . . . . 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...ThenStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 TUTORIAL 4-1: Examining an application that uses the If...Thenstatement . . 214 4.3 The If...Then...ElseStatement . . . . . . . . . . . . . . . . . . . . . . . . . . .217 TUTORIAL 4-2: Completing an application that uses the If...Then...Elsestatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 4.4 The If...Then...ElseIfStatement . . . . . . . . . . . . . . . . . . . . . . . . .220

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.