Table of Contents
| Ch. 1 | IDE | 1 |
| Ch. 2 | Controls in general | 67 |
| Ch. 3 | Program and module structure | 103 |
| Ch. 4 | Data types, variables, and constants | 129 |
| Ch. 5 | Operators | 169 |
| Ch. 6 | Subroutines and functions | 187 |
| Ch. 7 | Program control statements | 205 |
| Ch. 8 | Error handling | 233 |
| Ch. 9 | Introduction to Windows forms controls | 259 |
| Ch. 10 | Forms | 275 |
| Ch. 11 | Database controls and objects | 305 |
| Ch. 12 | Custom controls | 361 |
| Ch. 13 | Drag and drop, and the clipboard | 385 |
| Ch. 14 | OOP concepts | 407 |
| Ch. 15 | Classes and structures | 421 |
| Ch. 16 | Namespaces | 461 |
| Ch. 17 | Collection classes | 475 |
| Ch. 18 | Generics | 511 |
| Ch. 19 | Drawing basics | 521 |
| Ch. 20 | Brushes, pens, and paths | 563 |
| Ch. 21 | Text | 593 |
| Ch. 22 | Image processing | 615 |
| Ch. 23 | Printing | 635 |
| Ch. 24 | Reporting | 655 |
| Ch. 25 | Configuration and resources | 675 |
| Ch. 26 | Streams | 707 |
| Ch. 27 | File-system objects | 717 |
| Ch. 28 | Useful namespaces | 739 |
| App. A | Useful control properties, methods, and events | 763 |
| App. B | Variable declarations and data types | 775 |
| App. C | Operators | 781 |
| App. D | Subroutine and function declarations | 787 |
| App. E | Control statements | 789 |
| App. F | Error handling | 795 |
| App. G | Standard controls and components | 801 |
| App. H | Form objects | 905 |
| App. I | Classes and structures | 921 |
| App. J | Generics | 925 |
| App. K | Graphics | 927 |
| App. L | Useful exception classes | 939 |
| App. M | Date and time format specifiers | 943 |
| App. N | Other format specifiers | 947 |
| App. O | The application class | 953 |
| App. P | The My namespace | 957 |
| App. Q | Streams | 975 |
| App. R | File-system classes | 981 |
Read a Sample Chapter
Visual Basic 2005 Programmer's Reference
By Rod Stephens John Wiley & Sons
ISBN: 0-7645-7198-2
Chapter One
IDE This chapter describes Visual Studio's integrated development environment (IDE). It explains the most important windows, menus, and toolbars that make up the environment, and shows how to customize them to suit your personal preferences. It also explains some of the tools that provide help while you are writing Visual Basic applications.
Even if you are an experienced Visual Basic programmer, you should at least skim this material. The IDE is extremely complex and provides hundreds (if not thousands) of commands, menus, toolbars, windows, context menus, and other tools for editing, running, and debugging Visual Basic projects. Even if you have used the IDE for a long time, there are sure to be some features that you have overlooked. This chapter describes some of the most important of those features, and you may discover something useful that you've never noticed before.
Even after you've read this chapter, you should periodically spend some time wandering through the IDE to see what you've missed. Every month or so, spend a few minutes exploring the menus and right-clicking on things to see what their context menus contain. As you become a more proficient Visual Basic programmer, you will find uses for tools that you may have previously dismissed or failed to understand.
It is important to remember that the Visual Studio IDE is extremely customizable. You can move, hide, or modify the menus, toolbars, and windows; create your own toolbars; dock, undock, or rearrange the toolbars and windows; and change the behavior of the built-in text editors (change their indentation, colors for different kinds of text, and so forth).
These capabilities let you display the features you need the most and hide those that are unnecessary for a particular situation. If you need to use the Properties window, you can display it. If you want to make room for a very wide form, you can make it short and wide, and move it to the bottom of the screen. If you have a collection of favorite tools and possibly some you have written yourself, you can put them all in one convenient toolbar. Or you can have several toolbars for working with code, forms in general, and database forms in particular.
This chapter describes the basic Visual Studio development environment as it is initially installed. Because Visual Studio is so flexible, your development environment may not look like the one described here. After you've moved things around a bit to suit your personal preferences, your menus and toolbars may not contain the same commands described here, and other windows may be in different locations or missing entirely.
To avoid confusion, you should probably not customize the IDE's basic menus and toolbars too much. Removing the help commands from the Help menu and adding them to the Edit menu will only cause confusion later. It's less confusing to leave the menus more or less alone. Hide any toolbars you don't want and create new customized toolbars to suit your needs. Then you can find the original standard toolbars if you decide you need them later. The section "Customize" later in this chapter has more to say about rearranging the IDE's components.
This chapter describes the Visual Studio IDE. Before you can understand how to use the IDE to manage Visual Basic projects and solutions, however, you should know what projects and solutions are.
Projects and Solutions
A project is a group of files that produces some specific output. This output may be a compiled executable program, a dynamic-link library (DLL) of classes for use by other projects, or a custom control for use on other Windows forms.
A solution is a group of one or more projects that should be managed together. For example, suppose that you are building a server application that provides access to your order database. You are also building a client program that each of your sales representatives will use to query the server application. Because these two projects are closely related, it might make sense to manage them in a single solution. When you open the solution, you get instant access to all the files in both projects.
Both projects and solutions can include associated files that are useful for building the application but that do not become part of a final compiled product. For example, a project might include the application's proposal and architecture documents. These are not included in the compiled code, but it is useful to associate them with the project.
When you open the project, Visual Studio lists those documents along with the program files. If you double-click one of these documents, Visual Studio opens the file using an appropriate application. For example, if you double-click a file with a .doc extension, Visual Studio normally opens it with Microsoft Word.
To associate one of these files with a project or solution, right-click the project in the Solution Explorer (more on the Solution Explorer shortly). Select the Add command's Add New Item entry, and use the resulting dialog to select the file you want to add.
Often a Visual Basic solution contains a single project. If you just want to build a small executable program, you probably don't need to include other programming projects in the solution.
Another common scenario is to place Visual Basic code in one project and to place documentation (such as project specifications and progress reports) in another project within the same solution. This keeps the documentation handy whenever you are working on the application but keeps it separate enough that it doesn't clutter the Visual Studio windows when you want to work with the code.
While you can add any file to a project or solution, it's not a good idea to load dozens of unrelated files. While you may sometimes want to refer to an unrelated file while working on a project, the extra clutter brings additional chances for confusion. It will be less confusing to shrink the Visual Basic IDE to an icon and open the file using an external editor such as Word or WordPad. If you won't use a file very often with the project, don't add it to the project.
IDE Overview
Figure 1-1 shows the IDE immediately after starting a new project. The IDE is extremely configurable, so it may not look much like Figure 1-1 after you have rearranged things to your own liking.
If you don't have a reason to modify the IDE's basic arrangement, you should probably leave it alone. Then when you read a magazine article that tells you to use the Project menu's Add Reference command, the command will be where it should be. Using the standard IDE layout also reduces confusion when you need to consult with another developer. It's a lot easier to share tips about using the Format menu if you haven't removed that menu from the IDE.
The key pieces of the IDE are labeled with numbers in Figure 1-1. The following list briefly describes each of these pieces.
(1) Menus - The menus contain standard Visual Studio commands. These generally manipulate the current solution and the modules it contains, although you can customize the menus as needed. Visual Studio changes the menus, and their contents depending on the object you currently have selected. In Figure 1-1, a Form Designer (marked with the number 4) is open so the IDE is displaying the menus for editing forms.
(2) Toolbars - Toolbars contain tools that you can use to perform frequently needed actions. The same commands may be available in menus, but they are easier and faster to use in toolbars. The IDE defines several standard toolbars such as Formatting, Debug, and Image Editor. You can also build your own custom toolbars to hold your favorite tools. Visual Studio changes the toolbars displayed to match the object you currently have selected.
(3) Toolbox - The Toolbox contains tools appropriate for the item that you currently have selected and for the project type that you are working on. In Figure 1-1, a Form Designer is selected in a Windows Forms application so the Toolbox contains tools appropriate for a Form Designer. These include Windows Forms controls and components, plus tools in the other Toolbox tabs: Crystal Reports, Data, and Components (plus the General tab is scrolled off the bottom of the Toolbox). You can add other customized tabs to the Toolbox to hold your favorite controls and components. Other project types may display other tools. For example, a Web project would display Web controls and components instead of Windows Forms components.
(4) Form Designer - A Form Designer lets you modify the graphical design of a form. Select a control tool from the Toolbox, and click and drag to place an instance of the control on the form. Use the Properties window (marked with the number 6) to change the new control's properties. In Figure 1-1, no control is selected, so the Properties window shows the form's properties.
(5) Solution Explorer - The Solution Explorer lets you manage the files associated with the current solution. For example, in Figure 1-1, you could select Form1.vb in the Project Explorer and then click the View Code button (the icon third from the right at the top of the Solution Explorer) to open the form's code editor. You can also right-click an object in the Solution Explorer to get a list of appropriate commands for that object.
(6) Properties - The Properties window lets you change an object's properties at design time. When you select an object in a form designer or in the Solution Explorer, the Properties window displays that object's properties. To change a property's value, simply click the property and enter the new value.
(7) Error List - The Error List window shows errors and warnings in the current project. For example, if a variable is used and not declared, this list will say so.
If you look at the bottom of Figure 1-1, you'll notice that the Toolbox and Error List windows each have a series of tabs. The Toolbox's other tab displays the Document Outline window, which displays an outline view of a project showing its forms and components.
The Error List window's Output tab shows output printed by the application. Usually an application interacts with the user through its forms and dialogs, but it can display information here to help you debug the code. The Output window also shows informational messages generated by the IDE. For example, when you compile an application, the IDE sends messages here to tell you what it is doing and whether it succeeded.
The following sections describe the major pieces of the IDE in more detail.
Menus
The IDE's menus contain standard Visual Studio commands. These are generally commands that manipulate the project and the modules it contains. Some of the concepts are similar to those used by any Windows application (File\New, File\Save, Help\Contents), but many of the details are specific to Visual Studio programming, so the following sections describe them in a bit more detail.
The menus are customizable, so you can add, remove, and rearrange the menus and the items they contain. This can be quite confusing, however, if you later need to find a command that you have removed from its normal place in the menus. Some developers place extra commands in standard menus, particularly the Tools menu, but it is generally risky to remove standard menu items. Usually it is safest to leave the standard menus alone and make custom toolbars to hold customizations. For more information on this, see the section "Customize" later in this chapter.
Many of the menus' most useful commands are also available in other ways. Many provide shortcut key combinations that make using them quick and easy. For example, Ctrl-N opens the New Project dialog just as if you had selected the File\New Project menu command. If you find yourself using the same command very frequently, look in the menu and learn its keyboard shortcut to save time later.
Many menu commands are also available in standard toolbars. For example, the Debug toolbar contains many of the same commands that are in the Debug menu. If you use a set of menu commands frequently, you may want to display the corresponding toolbar to make using the commands easier.
Visual Studio also provides many commands through context menus. For example, if you right-click on a project in the Solution Explorer, the context menu includes an Add Reference command that displays the Add Reference dialog just as if you had invoked Project\Add Reference. Often it is easier to find a command by right-clicking an object related to whatever you want to do than it is to wander through the menus.
The following sections describe the general layout of the standard menus. You might want to open the menus in Visual Studio as you read these sections, so you can follow along.
Note that Visual Studio displays different menus and different commands in menus depending on what editor is active. For example, when you have a form open in the form editor, Visual Studio displays a Format menu that you can use to arrange controls on the form. When you have a code editor open, the Format menu is hidden because it doesn't apply to code.
File
The File menu, shown in Figure 1-2, contains commands that deal with creating, opening, saving, and closing projects and project files.
Following is a description of the commands contained in the File menu and its submenus:
New - The New submenu shown in Figure 1-2 contains commands that let you create a new Visual Basic project, Web site project (generally ASP.NET or a Web Service), or file (text file, bitmap, Visual Basic class, icon, and many others). The Project From Existing Code command creates a new project and puts all of the files in a directory in it, optionally including subdirectories.
New\File - The New submenu's File command displays the dialog shown in Figure 1-3. The IDE uses integrated editors to let you edit the new file. For example, the simple bitmap editor lets you set a bitmap's size, change its number of colors, and draw on it. When you close the file, Visual Studio asks if you want to save the file and lets you decide where to put it. Note that this doesn't automatically add the file to your current project. You can save the file and use the Project\Add Existing Item command if you want to do so.
Open - The Open submenu contains commands that let you open a project or solution, Web site, or file. The Convert command displays the Convert dialog shown in Figure 1-4. From this dialog, you can launch the Visual Basic 2005 Upgrade Wizard, which can help you convert Visual Basic 6 programs to Visual Basic 2005.
Close - This command closes the current editor. In Figure 1-2, Form1 is open in the form designer editor. This command would close this editor.
Close Project - This command closes the entire project and all of the files it contains. If you have a solution open, this command is labeled Close Solution.
Save Form1.vb - This command saves the currently open file, in this example, Form1.vb. Save Form1.vb As - This command lets you save the currently open file in a new file.
Save All - This command saves all modified files.
Export Template - This command displays the dialog shown in Figure 1-5. The Export Template Wizard lets you create project or item templates that you can use later.
Page Setup and Print - The Page Setup and Print commands let you configure printer settings and print the current document. These commands are enabled only when it makes sense to print the current file. For example, if you are viewing a source code file or a configuration file (which is XML text), you can use these commands. If you are viewing bitmap or a form in design mode, these commands are disabled.
Recent Files and Recent Projects - The Recent Files and Recent Projects submenus let you quickly reopen files, projects, and solutions that you have opened recently.
(Continues...)
Excerpted from Visual Basic 2005 Programmer's Reference by Rod Stephens Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.