ebook img

Microsoft Visual Basic 2010: For Windows, Web, and Office Applications: Complete PDF

769 Pages·2010·5.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 Microsoft Visual Basic 2010: For Windows, Web, and Office Applications: Complete

0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page i Microsoft® Visual Basic 2010 for Windows, Web, and Office Applications Complete Gary B. Shelly Corinne Hoisington Central Virginia Community College Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 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 a ffect 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 w ww.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest. Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 5522660099__0000__ffmm__ppii--ppxxxxvvii..iinndddd iiii 22//11//1100 1111::3377::4433 PPMM 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page ii Microsoft®Visual Basic 2010for Windows, Web, © 2011Course Technology, Cengage Learning and Office Applications: Complete ALL RIGHTS RESERVED. No part of this work covered by the copyright herein Gary B. Shelly may be reproduced, transmitted, stored or used in any form or by any means Corinne Hoisington graphic, electronic, or mechanical, including but not limited to photocopying, Vice President, Publisher: Nicole Pinard recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Executive Editor: Kathleen McMahon Section 107or 108of the 1976United States Copyright Act, without the prior Product Managers: Crystal Parenteau and Nada Jovanovic written permission of the publisher. Associate Product Manager: Aimee Poirier Editorial Assistant: Lauren Brody For product information and technology assistance, contact us at Director of Marketing: Cheryl Costantini Cengage Learning Customer & Sales Support, 1-800-354-9706 Marketing Manager: Tristen Kendall For permission to use material from this text or product, submit all requests online at cengage.com/permissions Marketing Coordinator: Stacey Leasca Further permissions questions can be emailed to Print Buyer: Julio Esperas [email protected] Production Director: Patty Stephan Content Project Manager: Heather Hopkins Library of Congress Control Number: 2010929660 Development Editor: Lisa Ruffolo ISBN-13: 978-0-538-46848-0 Copyeditor: Michael Beckett ISBN-10: 0-538-46848-3 Proofreader: Andrew Therriault Indexer: Sharon Hilgenberg Course Technology 20Channel Center Street QA Manuscript Reviewers: Serge Palladino Boston, MA 02210 and Danielle Shaw USA Art Director: Marissa Falco Cover Designer: Lisa Kuhn, Curio Press, LLC Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Cover Photo: Tom Kates Photography Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: Text Designer: Joel Sadagursky international.cengage.com/region Compositor: PreMediaGlobal Cengage Learning products are represented in Canada by Nelson Education, Ltd. To learn more about Course Technology, visit www.cengage.com/ coursetechnology To learn more about Cengage Learning, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online storewww.cengagebrain.com Microsoft is a registered trademark of Microsoft Corporation in the United States and/or other countries. Course Technology, a part of Cengage Learning, is an independent entity from the Microsoft Corporation, and not affiliated with Microsoft in any manner. Printed in the United States of America 1 2 3 4 5 6 7 16 15 14 13 12 11 10 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page iii C O N T E N T S Preface............................................................................................................ix CHAPTER 1 INTRODUCTION......................................................................................2 Introduction to Visual Basic Computer Programmers and Developers ................................................................5 Event-Driven Computer Programs with a Graphical User Interface ..........................5 2010 Programming BASIC PROGRAM OPERATIONS ..........................................................8 Input Operation................................................................................................8 Output Operation..............................................................................................9 Basic Arithmetic Operations ..............................................................................10 Logical Operations ..........................................................................................11 Saving Software and Data................................................................................16 VISUAL BASIC 2010 AND VISUAL STUDIO 2010 ..........................16 PROGRAMMING LANGUAGES ............................................................19 Visual Basic....................................................................................................19 C++ ..............................................................................................................19 Visual C# ......................................................................................................20 Visual F# ......................................................................................................20 .NET FRAMEWORK 4.0..........................................................................20 .NET Class Library ........................................................................................20 ADO.NET4.0................................................................................................22 ASP.NET4.0 ................................................................................................23 Microsoft Intermediate Language (MSIL) and Common Language Runtime (CLR) ......23 TYPES OF VISUAL BASIC 2010 APPLICATIONS ............................25 SUMMARY ................................................................................................25 KNOWLEDGE CHECK............................................................................26 CHAPTER 2 INTRODUCTION....................................................................................30 Program and Graphical USING VISUAL STUDIO 2010..............................................................32 Open Visual Studio 2010..................................................................................32 User Interface Design Create a New Visual Basic 2010 Windows Application Project................................34 Display the Toolbox..........................................................................................37 Permanently Display the Toolbox........................................................................38 View Object Properties......................................................................................39 Name the Windows Form Object........................................................................39 Set the Title Bar Text in a Windows Form Object..................................................42 Resize a Form ................................................................................................44 Add a Label Object ..........................................................................................45 Name the Label Object......................................................................................47 Change the Text in a Label Object ......................................................................48 Change Label Font,Font Style,and Font Size......................................................51 Center a Label Object in the Windows Form Object ..............................................53 Delete GUIObjects..........................................................................................55 Use the Undo Button on the Standard Toolbar ......................................................56 Add a PictureBox Object ..................................................................................56 Name a PictureBox Object ................................................................................58 Resize a PictureBox Object................................................................................58 Add a Second PictureBox Object ........................................................................60 Make Objects the Same Size ..............................................................................60 Align the PictureBox Objects..............................................................................62 Center Multiple Objects Horizontally in the Window ............................................64 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page iv iv Contents Add a Button Object ........................................................................................65 Name and Set Text for a Button Object................................................................67 Change Button Object Size................................................................................68 Add and Align a Second Button..........................................................................70 Align Objects Vertically ....................................................................................72 Save a Visual Basic Project................................................................................73 Close Visual Studio 2010..................................................................................74 Open a Visual Basic Project ..............................................................................75 PROGRAM DEVELOPMENT LIFE CYCLE..........................................75 Phase 1:Gather and Analyze the Program Requirements........................................76 Phase 2:Design the User Interface ......................................................................78 SAMPLE PROGRAM................................................................................80 Sample Program — Phase 2:User Interface Design................................................80 GUIDED PROGRAM DEVELOPMENT ................................................82 SUMMARY ................................................................................................94 LEARN IT ONLINE ................................................................................96 KNOWLEDGE CHECK............................................................................96 DEBUGGING EXERCISES ....................................................................97 PROGRAM ANALYSIS ............................................................................98 CASE PROGRAMMING ASSIGNMENTS ............................................99 CHAPTER 3 INTRODUCTION ..................................................................................112 Program Design SAMPLE PROGRAM..............................................................................112 FINE-TUNE THE USER INTERFACE ................................................114 and Coding BackColor Property........................................................................................114 Adding Images to a PictureBox Object................................................................117 Size an Image ..............................................................................................123 Visible Property ............................................................................................124 Enabled Property ..........................................................................................126 Running a Program ......................................................................................128 VISUAL BASIC PROGRAM CODING................................................129 Entering Visual Basic Code for Event Handling..................................................129 Visual Basic 2010 Coding Statements................................................................131 General Format ofa Visual Basic Statement........................................................133 IntelliSense ..................................................................................................134 Enter a Visual Basic Statement ........................................................................134 Set Visible Property to False ............................................................................138 Enabled Property ..........................................................................................141 Comments in Program Statements....................................................................143 Correcting Errors in Code ..............................................................................148 Additional Click Events..................................................................................153 Entering Code ..............................................................................................154 Close Procedure..............................................................................................154 Printing Code ..............................................................................................156 Coding Summary ..........................................................................................157 PHASE 3 — DESIGN THE PROGRAM PROCESSING OBJECTS....157 Event Planning Document..............................................................................157 PHASE 4 — CODE THE PROGRAM ..................................................159 GUIDED PROGRAM DEVELOPMENT ..............................................159 CODE LISTING......................................................................................176 SUMMARY ..............................................................................................178 LEARN IT ONLINE ..............................................................................179 KNOWLEDGE CHECK..........................................................................179 DEBUGGING EXERCISES ..................................................................180 PROGRAM ANALYSIS ..........................................................................182 CASE PROGRAMMING ASSIGNMENTS ..........................................183 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page v Contents v CHAPTER 4 INTRODUCTION ..................................................................................196 Variables and Arithmetic USER INTERFACE ................................................................................197 TextBox Objects ............................................................................................197 Operations Label Objects ................................................................................................208 Accept Button in Form Properties......................................................................210 Cancel Button in Form Properties ....................................................................211 Visual Studio Preparation for Code Entry..........................................................211 INTRODUCTION TO DATA ENTRY AND DATA TYPES................212 String Data Type ..........................................................................................213 Assignment Statements ..................................................................................214 Numeric Data Types ......................................................................................219 Other Data Types ..........................................................................................222 Literals........................................................................................................224 Forced Literal Types ......................................................................................225 Constants ....................................................................................................225 Referencing a Variable....................................................................................227 Scope ofVariables ..........................................................................................227 Converting Variable Data ..............................................................................228 Option Strict On ..........................................................................................231 ARITHMETIC OPERATIONS ..............................................................232 Arithmetic Operators......................................................................................233 Displaying Numeric Output Data....................................................................239 Clearing the Form — Clear Procedure and Focus Procedure ..................................242 Form Load Event ....................................................................................................244 Class Scope....................................................................................................247 Debugging Your Program................................................................................248 PROGRAM DESIGN ..............................................................................250 Event Planning Document..............................................................................252 Code the Program..........................................................................................253 GUIDED PROGRAM DEVELOPMENT ..............................................253 CODE LISTING......................................................................................271 SUMMARY ..............................................................................................272 LEARN IT ONLINE ..............................................................................274 KNOWLEDGE CHECK..........................................................................274 DEBUGGING EXERCISES ..................................................................276 PROGRAM ANALYSIS ..........................................................................277 CASE PROGRAMMING ASSIGNMENTS ..........................................279 CHAPTER 5 INTRODUCTION ..................................................................................292 Decision Structures CHAPTER PROJECT ............................................................................292 User Interface Design ....................................................................................294 Using the GroupBox Object ............................................................................294 Adding the RadioButton Objects ......................................................................298 Using the Checked Property ofRadioButton Objects ............................................300 Windows Application Container Objects............................................................302 Displaying a Message Box ..............................................................................303 String Concatenation ....................................................................................311 MAKING DECISIONS WITH CONDITIONAL STATEMENTS......312 Using an If...Then Statement........................................................................312 Relational Operators......................................................................................313 Comparing Different Data Types......................................................................317 Using the If...Then ...Else Statement............................................................318 Using the If...Then ...ElseifStatement..........................................................320 Nested IfStatements ......................................................................................322 Testing the Status ofa RadioButton Object in Code..............................................326 Block-Level Scope..........................................................................................327 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page vi vi Contents Using Logical Operators..................................................................................328 Select Case Statement......................................................................................332 Code Snippets................................................................................................337 Validating Data............................................................................................339 PROGRAM DESIGN ..............................................................................342 Event Planning Document..............................................................................343 Design and Code the Program..........................................................................344 GUIDED PROGRAM DEVELOPMENT ..............................................344 CODE LISTING......................................................................................355 SUMMARY ..............................................................................................356 LEARN IT ONLINE ..............................................................................358 KNOWLEDGE CHECK..........................................................................358 DEBUGGING EXERCISES ..................................................................359 PROGRAM ANALYSIS ..........................................................................361 CASE PROGRAMMING ASSIGNMENTS ..........................................364 CHAPTER 6 INTRODUCTION ..................................................................................378 Loop Structures CHAPTER PROJECT ............................................................................378 USER INTERFACE DESIGN ................................................................380 MenuStrip Object..........................................................................................380 InputBox Function ........................................................................................387 Displaying Data Using the ListBox Object ........................................................390 Accumulators,Counters,and Compound Operators ..............................................396 USING LOOPS TO PERFORM REPETITIVE TASKS ......................399 Repeating a Process Using the For...Next Loop....................................................399 Repeating a Process Using a Do Loop................................................................405 Do Until Loops ............................................................................................410 Avoiding Infinite Loops..................................................................................412 Priming the Loop ..........................................................................................413 Validating Data............................................................................................414 Creating a Nested Loop ..................................................................................416 Selecting the Best Loop....................................................................................417 USING A DATATIP WITH BREAKPOINTS ......................................417 PUBLISHING AN APPLICATION WITH CLICKONCE DEPLOYMENT........................................................................................425 PROGRAM DESIGN ..............................................................................430 Event Planning Document..............................................................................432 Design and Code the Program..........................................................................433 GUIDED PROGRAM DEVELOPMENT ..............................................433 CODE LISTING......................................................................................452 SUMMARY ..............................................................................................454 LEARN IT ONLINE ..............................................................................455 KNOWLEDGE CHECK..........................................................................455 DEBUGGING EXERCISES ..................................................................457 PROGRAM ANALYSIS ..........................................................................459 CASE PROGRAMMING ASSIGNMENTS ..........................................462 CHAPTER 7 INTRODUCTION ..................................................................................474 Creating Web Applications CREATING A WEB APPLICATION ....................................................476 Understanding the Active Server Page (ASP.NET 4.0) Platform ..............................476 Creating a Web Site with Visual Studio 2010............................................................477 Creating a Dynamic Web Site Using Visual Basic......................................................477 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page vii Contents vii USING A MULTI-PAGE STARTER TEMPLATE ................................479 Customizing the Master Page ..........................................................................480 Customizing Navigation Buttons on the Master Template ....................................482 Customizing the Default Page..........................................................................483 Adding Objects to the Web Page........................................................................483 Adding an Image Object to the Default Page ......................................................484 Entering Text Directly on the Web Page ............................................................486 CREATING AN ASP WEB FORM FOR THE RESERVATIONS PAGE..........................................................................489 Adding a Table for Alignment on a Web Form ....................................................490 Adding TextBox and Button Objects..................................................................491 Adding CheckBox Objects................................................................................493 Coding for CheckBox Objects............................................................................496 Adding a DropDownList Object ......................................................................496 Adding a Calendar Object ..............................................................................500 Specifying a Web Form Title............................................................................502 Code for a Calendar Object..............................................................................503 Adding Validation Controls ............................................................................504 Using the <br> Tag in Visual Basic Code............................................................514 USING STRING MANIPULATION PROPERTIES AND PROCEDURES IN THE STRING CLASS ................................515 Finding String Length ..................................................................................515 Using the Trim Procedure................................................................................516 Converting Uppercase and Lowercase Text ........................................................517 PROGRAM DESIGN ..............................................................................517 Event Planning Document..............................................................................519 Code the Program..........................................................................................520 GUIDED PROGRAM DEVELOPMENT ..............................................520 CODE LISTING......................................................................................532 SUMMARY ..............................................................................................534 LEARN IT ONLINE ..............................................................................535 KNOWLEDGE CHECK..........................................................................535 DEBUGGING EXERCISES ..................................................................537 PROGRAM ANALYSIS ..........................................................................538 CASE PROGRAMMING ASSIGNMENTS ..........................................539 CHAPTER 8 INTRODUCTION ..................................................................................552 Using Procedures and CHAPTER PROJECT ............................................................................552 Creating a Splash Screen ................................................................................554 Exception Handling Pausing the Splash Screen................................................................................560 Adding a ComboBox Object ............................................................................561 Determining the ComboBox Selected Index ........................................................564 Handling SelectedIndexChanged Events............................................................565 PROCEDURES........................................................................................567 Coding a Sub Procedure..................................................................................567 Passing Arguments ........................................................................................569 Function Procedures ......................................................................................575 Creating a Private Class-Level Variable............................................................578 EXCEPTION HANDLING....................................................................579 PROGRAM DESIGN ..............................................................................583 Program Design When Using Sub and Function Procedures ..................................585 Event Planning Document..............................................................................586 Design and Code the Program..........................................................................589 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 0538468483_Complete FM_REV2.qxd 8/3/10 10:55 AM Page viii viii Contents GUIDED PROGRAM DEVELOPMENT ..............................................589 CODE LISTING......................................................................................611 SUMMARY ..............................................................................................615 LEARN IT ONLINE ..............................................................................616 KNOWLEDGE CHECK..........................................................................616 DEBUGGING EXERCISES ..................................................................617 ANALYSIS EXERCISES ........................................................................619 CASE PROGRAMMING ASSIGNMENTS ..........................................622 ENRICHMENT CHAPTER INTRODUCTION ..................................................................................634 Visual Studio Tools CHAPTER PROJECT ............................................................................634 PROGRAMMING AN OFFICE DOCUMENT ....................................636 for Office Working with VSTO......................................................................................636 Using Templates............................................................................................636 Creating a VSTO Project................................................................................637 Understanding VSTO Toolbars,Menus,and Ribbons ..........................................639 Adding a DateTimePicker Object......................................................................641 Adding a Table to a Word Document..................................................................642 Inserting a Picture and Additional Cells in the Table............................................644 Using Table Styles to Format a Table ................................................................646 Adding the Actions Pane Control......................................................................648 Using a ComboBox Object ..............................................................................653 Calculating Loan Payments with the Pmt Function ............................................654 Calculating with the Math Class......................................................................657 PROGRAM DESIGN ..............................................................................658 DESIGN THE PROGRAM PROCESSING OBJECTS ........................660 GUIDED PROGRAM DEVELOPMENT ..............................................661 SUMMARY ..............................................................................................681 LEARN IT ONLINE ..............................................................................682 KNOWLEDGE CHECK..........................................................................682 DEBUGGING EXERCISES ..................................................................683 PROGRAM ANALYSIS ..........................................................................683 CASE PROGRAMMING ASSIGNMENTS ..........................................685 APPENDIX A UNICODE ..........................................................................................APP 1 APPENDIX B THE MY NAMESPACE ....................................................................APP 5 APPENDIX C NAMING CONVENTIONS............................................................APP 11 APPENDIX D USING LINQ WITH VISUAL BASIC 2010 ................................APP 13 APPENDIX E USING SILVERLIGHT WITH VISUAL BASIC 2010..................APP 21 Index......................................................................................................IND 1 Photo Credits ........................................................................................IND 26 Copyright 2010 Cengage Learning, All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.

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.