TRAINING & REFERENCE murach's Visual Basic 2008 Anne Boehm lbm Mike Murach & Associates, Inc. H 1-800-221-5528 (559) 440-9071 Fax: (559) 440-0963 murachbooks@murach.com www.murach.com
Contents Introduction xiii Section 1 introduction to Visual Basic programming Chapter 1 An introduction to Visual Studio 3 Chapter 2 How to design a Windows Forms application 35 Chapter 3 How to code and test a Windows Forms application 57 Section 2 The Visual Basic language essentials Chapter 4 How to work with numeric and string data 99 Chapter 5 How to code control structures 141 Chapter 6 How to code procedures and event handlers 167 Chapter 7 How to handle exceptions Chapter 8 How to work with arrays and validate data 189 and collections 223 Chapter 9 How to work with dates and strings 263 Chapter 10 More skills for working with Windows forms and controls 293 Chapter 11 How to create and use classes 331 Chapter 12 How to debug an application 369 Section 3 Database programming Chapter 13 An introduction to database programming 395 Chapter 14 How to work with data sources and datasets 427 Chapter 15 How to work with bound controls and parameterized queries 469 Chapter 16 How to use ADO.NET to write your Section 4 Object-oriented programming own data access code 505 Chapter 17 How to work with default properties, events, and operators 545 Chapter 18 How to work with inheritance 571 Chapter 19 How to work with interfaces and generics 609 Chapter 20 How to organize and document your classes 637 Section 5 Other programming skills Chapter 21 How to work with files and data streams 657 Chapter 22 How to work with XML files 681 Chapter 23 How to use LINQ 703 Chapter 24 How to enhance the user interface 729 Chapter 25 How to deploy an application 763 Reference aids Appendix A How to install and use the software and files for this book 789 Index 799
ExpelYid^cfl ootttottts Section 1 Chapter 1 Introduction to Visual Basic programming An introduction to Visual Studio An introduction to.net development 4 Windows Forms and Web Forms applications 4 Visual Studio and the.net programming languages 6 The.NET Framework 8 The Visual Studio IDE 10 How a Visual Basic application is compiled and run 12 How Visual Basic differs from the other.net languages 12 A tour of the Visual Studio IDE 14 How to start Visual Studio 14 How to open or close an existing project 16 Some possible menu variations 16 How to use the Form Designer 18 How to use the Code Editor 20 How to use the Solution Explorer 22 How to work with Visual Studio's windows 24 How to test a project 26 How to build a project 26 How to run a project 26 How to upgrade projects and change.net Framework versions 28 How to upgrade projects created in earlier versions of Visual Basic.NET 28 How to change the.net Framework version used by a project 30 Chapter 2 How to design a Windows Forms application How to set options and create a new project 36 How to set the Visual Studio options 36 How to change the import and export settings 36 How to create a new project 38 How to design a form 40 The design of the Invoice Total form 40 How to add controls to a form 42 How to set properties 44 Common properties for forms and controls 46 How to add navigation features 48 The property settings for the Invoice Total form 50 How to use Document Outline view 50 How to name and save the files of a project 52 How to name the files of a project 52 How to save the files of a project 52 Chapter 3 How to code and test a Windows Forms application An introduction to coding 58 Introduction to object-oriented programming 58 How to refer to properties, methods, and events 60 How an application responds to events 62
How to add code to a form 64 How to create an event handler for the default event of a form or control 64 How IntelliSense helps you enter the code for a form 66 The event handlers for the Invoice Total form 68 How to code with a readable style 70 How to code comments 72 How to detect and correct syntax errors 74 Other skills for working with code 76 How to use the toolbar buttons 76 How to collapse or expand code 76 How to print the source code 76 How to use code snippets 78 How to use the Smart Compile Auto Correction feature 82 How to use the My feature 84 How to get help information 86 How to run, test, and debug a project 88 How to run a project 88 How to test a project 90 - How to debug runtime errors 92 Section 2 Chapter 4 The Visual Basic language essentials How to work with numeric and string data How to work with the built-in value types 100 The built-in value types 10 How to declare and initialize variables 102 How to declare and initialize constants 102 How to code arithmetic expressions 104 How to code assignment statements 106 How to work with the order of precedence 108 How to use casting 110 How to change the type semantics 112 How to use the Math class 114 How to work with strings 116 How to declare and initialize a string 116 How to join and append strings 116 How to convert data types 118 The.NET structures and classes that define data types 118 How to use Visual Basic functions to convert data types 120 How to use methods to convert data types 122 How to use three of the formatting functions 124 How to use methods to convert numbers to formatted strings 126 Three other skills for working with data 128 How to work with scope 128 How to declare and use enumerations 130 How to work with nullable types I32 Two versions of the Invoice Total application 134 The basic Invoice Total application The enhanced Invoice Total application 134 136
Chapter 5 How to code control structures How to code Boolean expressions 142 How to use the relational operators 142 How to use the logical operators 144 How to code conditional statements 146 How to code If statements 146 How to code Select Case statements 148 An enhanced version of the Invoice Total application 150 How to code loops 152 How to code For loops 152 How to code Do loops 154 How to use Exit and Continue statements 156 Debugging techniques for programs with loops 158 The Future Value application 160 The design and property settings for the form 160 The code for the form 162 Chapter 6 How to code procedures and event handlers How to code and call procedures 168 How to code Sub procedures 168 How to call Sub procedures 170 When and how to pass arguments by reference and by value 172 How to code and call Function procedures 174 How to work with events 176 How to start an event handler for any event 176 How to handle multiple events with one event handler 178 How to use the Code Editor to start an event handler 180 How to add and remove event wiring 182 Another version of the Future Value application 184 The Function procedure 184 The event handlers 184 Chapter 7 How to handle exceptions and validate data An introduction to data validation and exception handling 190 How to use the IsNumeric function for data validation 190 How to display a dialog box for error messages 192 How exception handling works 194 How to use structured exception handling 196 How to catch an exception 196 How to use the properties and methods of an exception 198 How to catch specific types of exceptions 200 How to throw an exception 202 The Future Value application with exception handling 204 How to validate data 206 How to validate a single entry 206 How to use generic procedures to validate an entry 208 How to validate multiple entries 210 The Future Value application with data validation 212 The dialog boxes 212 The code 212 Two other ways to validate data 216 How to use the Validating event 216 How to use the masked text box 218
Chapter 8 224 How to work with arrays and collections How to work with one-dimensional, arrays How to create an array 224 How to assign values to the elements of an array 226 How to use For loops to work with arrays 228 How to use For Each loops to work with arrays 230 How to work with rectangular arrays 232 How to create a rectangular array 232 How to assign values to a rectangular array 232 How to work with rectangular arrays 234 How to work with jagged arrays 236 How to create a jagged array 236 How to assign values to a jagged array 236 How to work with jagged arrays 238 More skills for working with arrays 240 How to use the Array class 240 How to refer to and copy arrays 242 How to code procedures that work with arrays 244 How to work with collections 246 Commonly used collection classes 246 Typed vs. untyped collections 248 How to work with a list 250 How to work with a sorted list 254 How to work with queues and stacks 256 How to work with an array list 258 Chapter 9 How to work with dates and strings How to work with dates and times 264 How to create a DateTime value 264 How to get the current date and time 266 How to format DateTime values 266 How to get information about dates and times 268 How to perform operations on dates and times 270 How to use Visual Basic properties and functions to work with dates and times 272 How to work with strings 274 The properties and methods of the String class 274 Code examples that work with strings 276 More examples that work with strings 278 Two procedures for validating user entries 280 How to use the StringBuilder class 282 How to use Visual Basic functions to work with strings 284 How to format numbers, dates, and times 286 How to format numbers 286 How to format dates and times 288 Chapter 10 More skills for working with Windows forms and controls How to work with controls 294 Five more types of controls 294 How to work with combo boxes and list boxes 296 How to work with check boxes and radio buttons 300 How to work with group boxes 300 How to use Tab Order view to set the tab order 302 How to get the information you need for using a control 304
How to work with multi-form projects 306 How to add a form to a project 306 How to rename a form 306 How to change the startup form for a project 308 How to display a form as a dialog box 310 data between a form and a custom dialog box 312 How to pass How to use the MessageBox class 314 How to display a dialog box and get the user response 314 How to use the FormClosing event 316 The Payment application 318 The operation 318 The property settings 320 The code for the Customer form 320 The code for the Payment form 324 Chapter 11 How to create and use classes An introduction to classes 332 How classes can be used to structure an application 332 The members you can define within a class 334 The code for the Product class 336 How instantiation works 338 How to create a class 340 How to add a class file to a project 340 How to code fields 342 How to code properties 344 How to code methods 346 How to code constructors 348 How to code shared members 350 The Product Maintenance application 352 The operation of the Product Maintenance application 352 The classes used by the Product Maintenance application 354 The code for the Product Maintenance application 356 How to use the Class View window and class diagrams 360 How to use the Class View window 360 How to use class diagrams and the Class Details window 362 How to work with structures 364 How to create a structure 364 How to use a structure 364 Chapter 12 How to debug an application Basic debugging techniques 370 How to set the debugging options 370 How to work in break mode 372 How to use the Edit and Continue feature 372 How to use breakpoints 374 How to control the execution of an application 376 How to use the debugging windows 378 How to use the Locals window to monitor variables 378 How to use the Autos window to monitor variables 378 How to use Watch windows to monitor expressions 380 How to use the Immediate window to execute commands 382 How to use the Call Stack window to monitor called procedures 384 How to use the Output or Immediate window to get build or debugging information 386 How use the Visualizer dialog boxes to view strings 388
Section 3 Database programming Chapter 13 An introduction to database programming An introduction to client/server systems 396 The hardware components of a client/server system 396 The software components of a client/server system 398 An introduction to relational databases... 400 How a table is organized 400 How the tables in a database are related 402 How the columns in a table are defined 404 The design of the MMABooks database 406 How to use SQL to work with a relational database 408 How to query a single table 408 How to join data from two or more tables 410 How to add, update, and delete data in a table 412 An introduction to ADO.NET 414 The.NET data providers 414 How the basic ADO.NET components work 416 Concurrency and the disconnected data architecture 418 How a dataset is organized 420 How to work with data without using a data adapter 422 Two ways to create ADO.NET objects 424 Chapter 14 How to work with data sources and datasets How to create a data source 428 How to use the Data Sources window 428 How to start the Data Source Configuration Wizard 430 How to choose a data source type 430 How to choose the connection for a data source 432 How to create a connection to a database 434 How to save a connection string in the app.config file 436 How to choose database objects for a data source 438 The schema file created by the Data Source Configuration Wizard 440 How to use a data source 442 How to generate a DataGridView control from a data source 442 A Product Maintenance application that uses a DataGridView control 444 How to change the controls associated with a data source 446 How to generate detail controls from a data source 448 A Customer Maintenance application that uses TexlBox. controls 450 How to handle data errors 452 How to handle data provider errors 452 How to handle ADO.NET errors 454 How to handle data errors for a DataGridView control 456 How to use the Dataset Designer 458 How to view the schema for a dataset 458 How to use the Query Builder 460 How to preview the data for a query 462 How to interpret the generated SQL statements 464 Chapter 15 How to work with bound controls and parameterized queries How to work with bound text boxes and combo boxes 470 How to format the data displayed in a text box 470 How to bind a combo box to a data source 472 How to use code to work with a binding source 474
How to work with parameterized queries 476 How to create a parameterized query 476 How to use code to work with a parameterized query 478 How to work with the ToolStrip control 480 How to use the Items Collection Editor 480 How to code an event handler for a ToolStrip item 482 An enhanced Customer Maintenance application 484 The user interface 484 J The code 484 How to work with a DataGridView control 488 How to modify the properties of a DataGridView control 488 How to edit the columns of a DataGridView control 490 How to format the data in the columns of a DataGridView control 492 How to use a DataGridView control to create a Master/Detail form 494 A Customer Invoice Display application 496 The user interface 496 The dataset schema 496 The code for the Customer Invoice Display form 496 Chapter 16 How to use ADO.NET to write your own data access code How to work with connections and commands 506 How to create and work with connections 506 How to create and work with commands 508 How to create and work with parameters 510 How to use parameters in SQL statements 510 How to create parameters 512 How to work with parameters 514 How to execute commands 516 How to create and work with a data reader 516 How to execute queries that return a single value 518 How to execute action queries 518 A Customer Maintenance application that uses commands 520 The user interface 520 The class diagram for the business and database classes 522 The code for the CustomerDB class 524 The code for the StateDB class 530 The code for the MMABooksDB class 530 The code for the Customer Maintenance form 532 The code for the Add/Modify Customer form 536 Section 4 Object-oriented programming Chapter 17 How to work with default properties, events, and operators An introduction to the ProductList class 546 The code for a simple ProductList class 546 The specifications for the enhanced ProductList class 548 How to work with default properties 550 How to create a default property 550 How to throw an argument exception 552
How to work with events 554 How to define events 554 How to use events 554 How to overload operators 556 An introduction to operator overloading 556 How to overload arithmetic operators 558 How to overload relational operators 560 An enhanced version of the Product Maintenance application 562 The code for the ProductList class 562 The code for the Product Maintenance form 566 Chapter 18 How to work with inheritance An introduction to inheritance 572 How inheritance works 572 How the.net Framework uses inheritance 574 Methods inherited from the System.Object class 576 How to use inheritance in your applications 578 Basic skills for working with inheritance 580 How to create a base class 580 How to create a derived class 582 How polymorphism works 584 An inheritance version of the Product Maintenance application 586 The operation of the Product Maintenance application 586 The code for the Product, Book, and Software classes 588 The code for the ProductList class 592 The code for the Product Maintenance form 594 The code for the New Product form 596 Object types and casting 598 How to use the Type class to get information about an object's type 598 How to use casting with inheritance 600 How to work with abstract and sealed classes 602 How to work with abstract classes 602 How to work with sealed classes 604 Chapter 19 How to work with interfaces and generics How to work with interfaces 610 An introduction to interfaces 610 Some of the interfaces defined by the.net Framework 612 How to create an interface 614 How to implement an interface 616 A Product class that implements the ICloneable interface 618 How to use an interface as a parameter 620 How to work with generics 622 How to code a class that defines a generic collection 622 Some of the generic interfaces defined by the.net Framework 626 How to implement the IComparablefJ interface 628 How to use constraints 630 How to code an interface that uses generics 632 Chapter 20 How to organize and document your classes How to organize your classes 638 How to code multiple classes in a single file 638 How to split a single class across multiple files 640 How to work with namespaces 642
How to document your classes 644 How to add XML documentation to a class 644 How to view the XML documentation 646 How to create and use class libraries 648 How class libraries work 648 How to create a class library project 650 How to add a reference to a class library 652 How to use the classes in a class library 652 Section 5 Other programming skills Chapter 21 How to work with files and data streams An introduction to the System.lO classes 658 The classes for managing directories, files, and paths 658 How Fdes and streams work 660 How to use the FileStream class 662 How to use the exception classes for fde I/O 664 How to work with text files 666 How to write a text file 666 How to read a text file 668 A class that works with a text file 670 How to work with binary files 672 How to write a binary fde 672 How to read a binary file 674 A class that works with a binary file 676 Two other ways to work with files 678 Use the My.Computer.FileSystem object 678 Use the Visual Basic functions 678 Chapter 22 How to work with XML files An introduction to XML 682 An XML document 682 XML tags, declarations, and comments 684 XML elements 684 XML attributes 686 How to work with the XML Editor 688 How to create a new XML file 688 How to open an existing XML file 688 How to edit an XML file 688 How to work with XML 690 How to use the XmlWriter class 690 Code that writes an XML document 692 How to use the XmlReader class 694 How the XmlReader class reads nodes 696 Code that reads an XML document 698 A class that works with an XML file 700
Chapter 23 How to use LINQ Basic concepts for working with LINQ 704 How LINQ is implemented 704 Advantages of using LINQ 704 The three stages of a query operation 706 How to code a LINQ query 708 How to identify the data source for a query 708 How to filter the results of a query 710 How to sort the results of a query 712 How to select fields from a query 714 How to join data from two or more data sources 716 A Customer Invoice application that uses generic lists, 718 The user interface 718 The code for the form 720 A Customer Invoice application that uses a typed dataset 722 The dataset schema 722 The code for the form 724 Chapter 24 How to enhance the user interface Two types of user interfaces 730 A single-document interface (SDI) 730 A multiple-document interface (MDI) 730 How to develop SDI applications 732 How to use a startup form 732 How to use a Tab control 734 How to add menus to a form 736 How to create menus 736 How to set the properties that work with menu items 738 How to write code that works with menu items 738 How to develop MDI applications 740 How to create parent and child forms 740 How to write code that works with parent and child forms 742 How to add toolbars to a form 744 How to create a toolbar 744 How to write code that works with toolbars 746 How to create and use a status bar 748 How to create a simple status bar 748 How to create a status bar with multiple panels 750 How to work with modules 752 How to create and code a module 752 How to use a Main procedure 754 How to add help information 756 How to add tool tips 756 How to add context-sensitive help 756
Chapter 25 How to deploy an application An introduction to deploying Windows applications 764 How XCopy works 764 How ClickOnce works 764 How a Setup program works 764 How to use XCopy 766 How to create a release build 766 How to copy the release build to the client 766 How to use ClickOnce 768 How to publish an application 768 How to select the files that are published 770 How to select the prerequisites 770 How to set the update options 772 How to set the publish options 772 How to install an application 774 How to update an application 774 How to create and use a Setup program 776 How to create a Setup project 776 How to add output files to a Setup project 778 How to complete a Setup project 780 How to use a Setup program to install an application 782 How to deploy database applications 784 Using ClickOnce deployment 784 Using a Setup program 786 Appendix A How to install and use the software and files for this book How to use the downloadable files 790 How to install Visual Studio 2008 792 How to install and use SQL Server 2005 Express 794 How to attach the database for this book to the database server 796