Platinum Edition Using Visual Basic 5

Previous chapterNext chapterContents


- 2 -
Introduction to the Development Environment

As old-timers will note, the development environment is completely different from the one in previous versions of Visual Basic. The environment is, however, consistent with the development environments of Microsoft's other programming languages. We'll look closely at the development environment in the section "Exploring the Visual Basic Interface," but first let's take a brief look at the key features of the new environment.

If you have used previous versions of Visual Basic, you will discover that almost everything about the interface has changed. I found that, after an initial adjustment period, these changes added a lot of flexibility to the development environment.
The form and code windows are now set up as MDI child windows. The new style allows you to easily switch from one window to another, keeping your toolbar and menus available at all times.
This means that you can have the Properties window, Project window, and toolbox docked against the edge of your screen or floating somewhere in the middle.
Visual Basic 5 has new and improved toolbars that make it easier to accomplish many of your programming tasks, such as lining up controls precisely on a form.
In addition to detailed help files with context-sensitive links, Visual Basic 5 includes many detailed VB articles in the Books Online.

Understanding the Environment's Key Features

A significant change in VB5 is its use of a Multiple-Document Interface (MDI). If you are unfamiliar with MDI, you can compare it to having multiple documents open in Microsoft Word. Each document is contained in a child window which is, in turn, contained within the main parent window. As with other MDI applications, such as Word or Excel, you can choose to have the child document fill the whole window or have multiple windows visible simultaneously.


TIP: If you are not already using a large monitor (17" or greater) at high resolution, get one! While the new interface is great, you need a lot of screen space to use it most effectively. I recommend a screen resolution of 800 x 600 as a minimum or 1024 x 768 if your video card (and eyes) can support it.

Next, you can edit more than one project in the same Visual Basic session. This means you don't have to close one project in order to open and make changes to another. This is very convenient if you are developing projects that interact with each other, because you can save and compile all projects in a group with a single menu option.

Another new feature is dockable toolbars and windows. This means that a window can be floating in the middle of the screen or docked along one of the edges. Dockable windows include the toolbars and windows with small title bars, such as the toolbox.

In addition to the window management enhancements, Visual Basic has some useful tools to make code entry easier. Microsoft calls them Auto List Member and Auto Quick Info; you will call them fantastic. If you have trouble remembering MessageBox constants, control properties, or even the parameters to your own functions, the code editor now presents the information automatically while you type! For example, as soon as you press the spacebar after a MsgBox function call, you are presented with the parameters in a ToolTip-like format with a drop-down box of the available constants.


TIP: When you are presented with the drop-down box mentioned in the preceding paragraph, you don't have to use the mouse to select an item. Just keep typing (or use the arrow keys) until the list item you want is highlighted; then press the spacebar, comma (,), Enter, or Tab, and continue with your program!

Starting Up

When you start Visual Basic, you see the ew Project dialog box, shown in Figure 2.1. This dialog box has the following three tabs:

FIG. 2.1
The New Project dialog box's New tab lets you select from several types of projects to create.

If you choose to create a new project, Visual Basic creates the appropriate project template for you based on your selection from the New Project dialog box. You can choose to create one of these project types:

The Visual Basic Work Area

After you select the project type from the New Project dialog box, you are presented with the design environment. This is where you do the work of actually creating your masterpiece application. The basic design environment is shown in Figure 2.2. This is probably what Visual Basic 5 looked like when you started it for the first time.

FIG. 2.2
The Visual Basic desktop provides an assortment of tools that you can use to create programs.

As you can see, Visual Basic shares a lot of elements with other Windows programs. The toolbars and menus look similar to those in Office 97. A few of the menu items are the same: File, Edit, Help, and others.

Using the Menu Bar

Many programmers want to find quick keyboard shortcuts for frequently used tasks. As with other Windows programs, the menus at the top of the Visual Basic screen can be displayed by holding down the Alt key while pressing the appropriate underlined character in the menu bar. After the initial menu is displayed, simply press the underlined character of a menu item to select it. For example, press Alt+F to open the File menu, and then press P to choose the Print command.

Visual Basic also utilizes several shortcut keys that let you bypass the menu entirely. Most of these are listed to the right of their respective menu items. For example, in the View menu you might notice F2 to the right of Object Browser. This means that you can see the Object Browser by pressing F2. Although some quick shortcuts are listed in Table 2.1, a quick perusal of the menus would definitely be worth your while.

Table 2.1 Shortcut Keys

Menu Item Shortcut Key Description
Edit, Cut Ctrl+X Removes the selected text or control from its current location and copies it to the Clipboard.
Edit, Copy Ctrl+C Makes a copy of the selected text or control on the Clipboard, but does not remove it from its original location.
Edit, Paste Ctrl+V Pastes the contents of the Clipboard to the active form or code window.
Edit, Undo Ctrl+Z Undoes the last change.
Edit, Find Ctrl+F Finds a piece of text. (You must be in an edit window to use this.)
File, Open Ctrl+O Opens a project.
File, Save Ctrl+S Saves the current file.
File, Print Ctrl+P Displays the Print dialog box, from which you can print the current form or module or the entire application.
View, Project Ctrl+R Shows the Project Explorer window Explorer (if it's not already displayed).
View, Properties F4 Shows the Properties window (if it's not already Window displayed).


TIP: Experimentation is encouraged! In addition to the preceding list, Visual Basic includes some not-so-obvious tricks such as Ctrl+Y (delete a line of code), which is apparently an homage to WordStar.

Examining the Changes in Familiar Menus The File menu is basically the same as it was in previous versions of Visual Basic (see Figure 2.3). However, because Visual Basic 5.0 allows multiple projects on the desktop, some project management functions have been moved to the new Project menu. The Edit menu has the standard features you would expect, such as Cut, Copy, and Paste, but also has quite a few new features that are associated with the new capabilities of the code editor (see Figure 2.4). These features give you the following capabilities:

FIG. 2.3
Visual Basic's File menu assists you in managing the files that make up your projects.

FIG. 2.4
The Edit menu offers quite a few commands to help you write code.

The View menu provides you with access to all the parts of the Visual Basic interface. From the View menu, you can display and hide various parts of the design environment. These can include toolbars, design windows, and windows containing program code.

The Project menu contains many of the functions you need to manage an individual project. From this menu, you can add and remove project elements--such as forms, modules, and user controls. The Project menu also provides you with access to the Components dialog box, where you select the controls to be used in the project, and to the Project Properties dialog box, where you select things like the startup form and compiler options.

The other familiar menus--Run, Tools, Add-Ins, and Help--are all similar to their counterparts in earlier versions of Visual Basic.

Checking Out the New Menus Three new menus have been added to Visual Basic 5: Format, Debug, and Window. While the Window menu is new to Visual Basic, it should be familiar to you if you have worked with other Windows programs. This menu lets you arrange and select multiple form and code windows. The Debug menu contains tools that help you track down problems in your code (see Figure 2.5). From here, you can set breakpoints and watches, as well as control how the code is executed in debug mode. It is interesting to note that the Debug window from previous versions is now called the Immediate window and commands can be executed here even when the program is not running.

FIG. 2.5
The Debug menu provides numerous commands to aid in troubleshooting your code.

The final new menu is the Format menu, shown in Figure 2.6. This menu provides you with a number of features for working with groups of controls on your form. These features make it easy to align multiple controls, set the height or width of multiple controls to make them consistent, center a group of controls, or adjust the spacing between controls. If you have worked in previous versions of Visual Basic (and even if you haven't), you will really appreciate the power of these features.

See "Using Controls," Chapter 4

FIG. 2.6
The Format menu helps you with the placement and sizing of controls on your forms.

Accessing Functions with the Toolbars

Visual Basic's toolbars provide you with quick access to some of the functions you will use most often. There are four toolbars available (see Figure 2.7):

FIG. 2.7
Visual Basic's Standard toolbar is docked below the menu bar; the other toolbars are floating on the desktop. The toolbars can be modified from the Customize dialog box.

The Standard toolbar is the only one displayed the first time you start Visual Basic. You can specify which toolbars are displayed by choosing Toolbars from the View menu or by right-clicking any visible toolbar. Any of them can be free-floating or "docked" just below the menu bar; their startup positions will be the same as the last time you exited Visual Basic. Selecting View, Toolbars, Customize enables you to modify the existing toolbars or even create your own.

Visual Basic's toolbars follow the standard used by the latest generation of programs in that they provide you with ToolTips. A ToolTip is a little yellow box that pops up if you let the mouse pointer hover over a button for a few seconds; it contains a description of the underlying button's function.


TIP: Starting with version 5.0, ToolTips also display the value of a variable in a code window. Use this feature by letting the mouse pointer hover over a variable name while in break mode. This is a real time-saver if you are used to setting up a watch or printing values in the Immediate window.

Display all the toolbars as explained in the preceding paragraph and move the pointer over the buttons to familiarize yourself with them. Remember that you can always use the ToolTip feature when you're unsure which button is which.

Two of these buttons require special attention. The Add Project button and the Add Form button both invoke drop-down lists of items (see Figure 2.8). If you select one of these items, the default item for the button changes to the type of item that you selected.

FIG. 2.8
Drop-down buttons enable you to specify the type of project or file to be added.

The Add Project button enables you to add a project to the desktop. This can be one of the following four types:

The Add Form button lets you add any of the following pieces to your current project:

We address most of the other buttons on the toolbar a little later. However, two special areas on the toolbar deserve mentioning. At the far right of the toolbar are two blocks with a pair of numbers in each block. These two blocks show the position and size of the form or control with which you are working. The two numbers in the first block indicate the horizontal and vertical positions, respectively, of the upper-left corner of the current object, as measured from the upper-left corner of the screen (if the current object is a form), or of the current form (if the current object is a control). The two numbers in the second block show the horizontal and vertical dimensions, respectively, of the current object. These numbers are not visible, however, when editing in a code window.


NOTE: Both the position and dimension information are given in twips. A twip is a unit of measure that Visual Basic uses to ensure that placement and sizing of objects is consistent on different types of screens. A twip is equal to 1/20 of a printer's point; there are approximately 1,440 twips in a logical inch (the amount of screen space that would take up one inch when printed).

One final note about the toolbars. If you don't like having them located at the top of the screen, you can move any of them by clicking the double bars at the left edge and dragging to a new location. You can park a toolbar against any other edge of the desktop or leave them floating in the middle as shown in Figure 2.9.

FIG. 2.9
Even the Standard toolbar can float freely on the desktop.

Organizing Visual Basic's Controls

The controls that are used in Visual Basic are the heart and soul of the programs that you create. The controls enable you to add functionality to your program quickly and easily. There are controls that enable you to edit text, connect to a database, retrieve file information from a user, or display and edit pictures.

Obviously, with all these controls available, you need a way to keep them organized. This is the function of the toolbox (see Figure 2.10). This toolbox contains buttons representing the controls that are available for use in your program. (A list of the basic set of Visual Basic 5 controls is contained in Table 2.2.) Clicking one of the control "tools" enables you to draw a control of that type on a form. Double-clicking a tool places a default-sized control of that type in the center of the current form. Clicking the Pointer tool in the upper-left of the toolbox cancels a pending control-drawing function and restores the mouse pointer's normal functionality.

FIG. 2.10
The basic control set that is available when you first start Visual Basic The toolbox can be moved around on-screen to a location that is convenient for you.

Table 2.2 Standard Visual Basic Controls

Control Button

Control Name Function

PictureBox Displays a graphic image.

Label Displays text that the user cannot directly modify.

TextBox Displays text that the user can edit.

Frame Provides a method for grouping controls. (To group controls in a frame, select the frame with a single-click first, then draw a control in it.)

CommandButton Enables the user to initiate a program action. Can include an icon, caption, and ToolTips.

CheckBox Displays or enables input of a two-part choice, such as Yes/No or True/False.

OptionButton Displays or enables a choice among multiple items. (Also known as a radio button.)

ComboBox Enables the user to select an entry from a list or enter a new value.

ListBox Displays a list of items from which the user can select one or more entries.

HscrollBar (Horizontal Scroll bar) Produces a numerical value based on the scroll bar's horizontal position.

VscrollBar (Vertical
Scroll bar)
Same as above but vertical. Note the scrollbars behave like standard Windows scrollbars.

Timer Provides a means for an action to be taken after passage of a certain amount of time.

DriveListBox Displays and enables a user to choose from available disk drives on the computer.

Dir ListBox Displays and enables a user to choose from available subdirectories on a drive.

FileListBox Displays and enables a user to choose from available files in a directory.

Shape Displays geometric shapes on the form.

Line Displays lines on the form.

Image Displays a graphic image. Similar in appearance to the picture control but with different functionality.

Data Provides a link to database files.

OLE Provides you with a way to link to OLE servers.

You can add other controls to the toolbox by selecting the Components item from the Project menu. This brings up the Components dialog box (see Figure 2.11). This dialog box enables you to choose any additional controls (OCXs) that have been installed on your system. If you choose to add a control to the toolbox, it appears in the toolbox after you choose the OK or Apply button.

FIG. 2.11
Controls are added to the toolbox with the Components dialog box.


TIP: You can also access the Components dialog box by right-clicking the mouse on the toolbox and then selecting the Components item from the pop-up menu.

By default, all the components for your project will appear in the toolbox in one big group. However, if you use a lot of controls, this can make it very difficult to manage all of them. To help with this problem, Visual Basic enables you to add tabs to the toolbox. (It has one tab, General, by default.) To add a tab, right-click the toolbox, select Add Tab from the pop-up menu, and give the new tab a name. You can then move controls from one tab to another and group your controls in the way that is most convenient to you. Figure 2.12 shows the toolbox with a Data Access tab added to it.

FIG. 2.12
A new feature in Visual Basic 5 gives you the ability to group control tools using custom tabs in the toolbox.

The Canvas of Your Programs

The windows you design in your Visual Basic programs are known as forms. The form can be thought of like an artist's canvas. You use elements in the toolbox to "draw" your user interface on a form.

The form is part of the desktop and is your primary work area for creating the user interface. If you look closely at the form in Figure 2.13, you might notice that the form has dots on it. These dots form a grid whose purpose is to help you position controls on the form; it is invisible when your program is running. You can control the spacing of the grid dots from the Tools, Options, General dialog box. You can also choose not to display the grid at all.

FIG. 2.13
When designing a form in Visual Basic, a grid is available to help you easily line up controls.

Controlling Your Forms and Controls

The Properties window is an important part of the Visual Basic desktop. It shows all the available properties for the currently selected form, control, or module (see Figure 2.14). If the Properties window isn't visible, first select the object(s) whose properties you want to view or change and then press F4. It can also be viewed by selecting Properties Windows from the View menu or by right-clicking an object and choosing Properties from the context-sensitive menu that pops up.

FIG. 2.14
The Properties window provides an easy way to change the properties that govern the appearance and behavior of controls.

Properties determine how a form or control looks and how it behaves in a program. The Properties window lists all of the currently selected object's properties that can be changed at design time, as opposed to runtime properties, which can be changed only during program execution. Many properties can be changed either at design time or at runtime.

An example of a property is the Caption property of a Label control. It can be changed by simply typing Hello World into the Caption field in the Properties window (a design time change) or with a statement in your code like Form1.Label1.Caption = "Hello World" (a runtime change).

The Properties window has two tabs on it. These tabs enable you to group the properties either alphabetically or by logical categories. Another improvement to the Properties window is that it now includes a description of the selected property in a pane at the bottom. This avoids much of the need to look up properties in the Help system.


NOTE: In a change from previous versions of Visual Basic, the Name property of any object appears at the top of the list of properties, rather than in its proper alphabetical order.


NOTE: Many controls have an entry in the Properties window labeled (Custom). This brings up a special Property Page dialog box containing all of the design-time properties for that control in an easy-to-modify format.

Using the Project Window

Another window on the desktop is the Project window. This window shows a list of all the forms and code modules that are used in your program. Figure 2.15 shows an example of a Project window. If you want to view a form or code module, double-click it here during design time, or click it once and click the View Form or View Code button.

FIG. 2.15
The Project window shows the files that make up the open project(s).


NOTE: One way to think of a project is as a group of related files. The Project brings together all the files needed to create your program.

When you save a project, you're basically saving a list of the various files that make up a project. The project file itself is stored with a default extension of .VBP (Visual Basic Project). Several other types of files make up the components of the projects. Some of the more common types are listed in Table 2.3.

Table 2.3 Visual Basic File Types

File Type Extension
Visual Basic Form .FRM
Code Module .BAS
Class Module .cLS
User-Created Control .CTL
ActiveX Document Form File .DOB

The Project window uses an outline list to show you not only the Forms and Code modules in the open project(s) but also any Class modules, User-defined controls, or Property pages. There are two ways to view your project. The folder view, accessible by clicking the left button, displays the parts of your project organized by category. On the other hand, clicking the rightmost button lists the elements of your project based on their associated file names.

Where Work Gets Done

The final piece of the desktop is one or more Code windows. Code windows are where you do all the entry and editing of program code that enables your programs to actually perform tasks (see Figure 2.16). Each form has its own associated Code window. A project can also contain a couple of types of stand-alone Code windows known as modules. To access a Code window, you can double-click a form or one of its objects, or you can click the View Code button in the Project window while the appropriate object is highlighted.

FIG. 2.16
The Code window is where you enter and edit the instructions that perform the work of your program.

Customizing Your Environment

As we've discussed, the Visual Basic development environment is highly customizable. Most of the windows and toolbars in Visual Basic can be placed at the edges of the main program window, or they can float anywhere on the screen. You can position and resize the windows to fit your preferences; the next time you start Visual Basic, the environment will be as you left it. Figure 2.17 shows you one way the development environment can be rearranged.

FIG. 2.17
The various pieces of Visual Basic's development environment can be arranged in many ways.

Getting Help When You Need It

As you work with the many features of Visual Basic, you may need more information about a particular command or object than is provided in this book. Fortunately, Microsoft has included a great online help system with Visual Basic 5. In addition to a searchable index, context-sensitive help is available from the development environment. This means you can highlight a word or object and press the F1 key to bring up related information.

The Basic Help System

The easiest way to access the Help system is through the Help menu. There are several choices available on the menu for Visual Basic help:

Select Microsoft Visual Basic Help Topics to enter the main online Help system. The Contents tab you find there displays the main table of contents for the Help system, as shown in Figure 2.18. From here, you can choose topics of interest and navigate through the other parts of the Help system. You choose a topic by clicking one of the hypertext links (similar to those you find when browsing the World Wide Web).

FIG. 2.18
The Help system's Contents page displays a road map of the topics available.

Clicking the Help system's Index tab displays an extensive listing of help topics available (see Figure 2.19). To find a specific item, type part of the name of the item in the window's text box until the desired topic appears in the list and then click the topic in the list. You can also use the scroll bar in the list box to locate a specific topic.

FIG. 2.19
The Help system's Index page helps you locate specific topics.

While the Index page allows a quick selection based on topic name, the Find page goes one step further. It has the additional capability of searching for a specific word or words within all of the available topics. The first time you use the Find option, a Word List will be created. Then you can type in a word or phrase and list the help topics that contain it. The Find screen is shown in Figure 2.20.

FIG. 2.20
From the online Help system's Find tab, you can search the help file for specific words.

Context-Sensitive Help

In addition to letting you look up information in the Contents, Index, and Find pages, Visual Basic provides you with context-sensitive help. This enables you to directly get the help you need for a particular control or code keyword while you're working with that object or in the Code window. To get help for any control, simply select the control on your form and then press the F1 key. The Help system displays information about the control. This help page, an example of which is shown in Figure 2.21, also provides links to detailed descriptions of the properties, methods, and events of the control.

FIG. 2.21
The Help page for a control provides a description of the control as well as links to related pages.

For code keywords, simply place the cursor in the word in the Code window and then press F1. The Help system goes directly to the page for that command and provides you with the syntax of the command, as well as other information about it. In addition, for most commands, a link is provided that gives you an example of how the command is used.


TIP: When you find the help topic for a command you're working with, you can copy sample code from the help screen to the Clipboard and then paste it directly into your application's Code window. Use Ctrl+C and Ctrl+V, respectively, to copy and paste the text.

From Here...

This chapter introduced you to Visual Basic's development environment. You now know the basic methods for manipulating code and forms. As with any skill, the best way to learn is to practice. Think of some ideas for example programs and try writing them. Sample programs with varying levels of complexity are presented in this book; you can also find some in Visual Basic's Samples folder. On the other hand, if you would like to learn even more about the design environment, there are more chapters that explore it extensively. A couple of good places to go next are:


Previous chapterNext chapterContents


Macmillan Computer Publishing USA

© Copyright, Macmillan Computer Publishing. All rights reserved.