Using Visual Basic 6

Previous chapterNext chapterContents


- 4 -
Using the Intrinsic Controls


What Are Forms and Controls?


Are there programs you can't see?

Although most programs you write will start with a form, not all programs use forms. These programs don't have any way for a person to use them directly; instead, other programs use them. Don't worry about this yet. Right now, just remember that people will be using your program and will use the controls you put on a form.




Distributing intrinsic controls

When you distribute a program that includes intrinsic controls only, you don't need to include OCX files. All the intrinsic controls are contained within the Visual Basic runtime files, so they'll be available to your program as long as the runtime files are present on the system.


In Visual Basic, forms are the foundations you generally use to build programs. A form is where you put all the things that people interact with as they use your program. Those things you put on the form are controls, which enable the people who use your program to do things, such as enter text and click buttons.

Think of it this way: If you build a house, you start with a foundation--think of this as the form. On top of the foundation, you add all the things that allow you use the house: a floor, walls, and doors. These things are the controls.

What Are Intrinsic Controls?

For the most part, you'll use a relatively small set of controls when you program in Visual Basic. However, these controls are very powerful. With them, you can add buttons, check boxes, labels, and text boxes to your programs. You can use them to see files on your hard drive right from your program. You can even read a database! These basic controls are intrinsic controls, and they're available in every edition of Visual Basic 6.

The intrinsic controls are available whenever you use Visual Basic. During design time, you can access them from the Toolbox (see Figure 4.1). Table 4.1 lists the intrinsic controls.

FIGURE 4.1 By using only the intrinsic controls, you can create powerful programs. To select a control, just click it.

TABLE 4.1  The Visual Basic 6 Intrinsic Controls

Control Description
Label Displays text on a form
Frame Serves as a container for other controls
CheckBox Enables users to select or deselect an option
ComboBox Allows users to select from a list of items or add a new value
HscrollBar Allows users to scroll horizontally through a list of data in another control
Timer Lets your program perform actions in real time, without user interaction
DirListBox Enables users to select a directory or folder
Shape Displays a shape on a form
Image Displays graphics (images) on a form but can't be a container
OLE Container Enables you to add the functionality of another Control program to your program
PictureBox Displays graphics (images) on a form and can serve as a container
TextBox Can be used to display text but also enables users to enter or edit new or existing text
CommandButton Enables users to initiate actions
OptionButton Lets users select one choice from a group; must be used in groups of two or more
ListBox Enables users to select from a list of items
VscrollBar Enables users to scroll vertically through a list of data in another control
DriveListBox Lets users select a disk drive
FileListBox Lets users select a file
Line Displays a line on a form
Data Lets your program connect to a database

Adding and Removing Controls

You can add controls to a form in two ways: by double-clicking and by drawing. You learned about double-clicking in Chapter 1, "Building Your Application;" whenever you double-click an icon on the toolbar, the associated control appears on your form. When you do this, though, you can't control where the control goes: You're at the mercy of Visual Basic. When you draw a control on your form, you can put it wherever you want it.

Draw a control on a form

1. Click the control's Toolbox icon.

2. Move the mouse pointer over your form. Notice that your pointer is now shaped as a crosshair instead of an arrow. Click (and hold) the mouse button where you want the control to go.

3. Drag the mouse down slightly and to the left. As you move the mouse, notice that a box starts to appear (see Figure 4.2).

4. When the box is the proper size, let go of the mouse button. The control you selected now appears on the form.

FIGURE 4.2 When drawing controls, use the box to approximate the size of your control.\

Remove a control from a form

1. Select the control you want to delete by clicking it. The control you select will appear with a box at each corner and side (see Figure 4.3).

2. Press the Delete key.

You can also remove a control by right-clicking it. From the context menu that appears, select Delete.

FIGURE 4.3 You know a control is selected when boxes called sizing handles surround it.

The Importance of Naming Controls

A control's name is one of its most important attributes because you literally refer to a control by its name whenever you want it to do something. Names are so important that every time you put a control on your form, Visual Basic automatically gives a name to it. If you add a CommandButton, Visual Basic names it Command1; if you add a TextBox, it's automatically named Text1.

However, naming controls like this can be confusing. For example, if you add six CommandButtons to your form, Visual Basic will name them Command1, Command2, Command3, and so on. If you need 100 buttons, Visual Basic will name the last one Command100. How are you supposed to remember what Command67 does? The trick is, rather than let Visual Basic name your controls automatically, you should do it yourself.

Name a control

1. After you add a control to a form, make sure that it's selected (it has a box at each corner and side when it's selected).

2. In the Properties window, click the control's name in the right column (see Figure 4.4).

3. Delete the current name and add the name you want.

A better name for a control is one that tells not only what type of control it is, but also what it does within your program. Can you see the value here? If you consistently give your controls descriptive names, you'll always know what they do. Naturally, there is a convention you can use to help you with this.

FIGURE 4.4 You can change a control's name in the Properties window.


Friendly names

When naming a control, the first letter of the friendly name is generally uppercase. This makes it easier to read the control's name, because you can easily differentiate between the friendly name and the control's abbreviation.


This convention is quite simple. It consists of a short (usually three-letter) abbreviation that identifies the type of control (see Table 4.2), followed by a longer, friendly name that describes what the control does within your program. The abbreviation is lowercase, and the friendly name follows it immediately, without any spaces.

TABLE 4.2  Common Prefixes for Naming Visual Basic Intrinsic Controls

Control Prefix Control Prefix
Label lbl PictureBox pic
Frame fra TextBox txt
CheckBox chk CommandButton cmd
ComboBox cbo OptionButton opt
HscrollBar hsb ListBox lst
Timer tmr VscrollBar vsb
DirListBox dir DriveListBox drv
Shape shp FileListBox fil
Image img Line lin
OLE Container Control ole Data dat

For example, a CommandButton (which has the abbreviation cmd) used to close a program might be named cmdClose. If you use a TextBox (which has the abbreviation txt) for users to input their name, you might call it txtName. This convention becomes especially powerful when you use the same type of control multiple times. Say that you use two text boxes to capture a user's name. If you let Visual Basic name the controls, they will be Text1 and Text2. If you take over and name them yourself, you can name them txtFirstName and txtLastName.

As you write Visual Basic programs, it's very important to name controls properly and consistently. As a programmer, though, you probably already know this.

How to Size and Position a Control

When you're drawing controls on a form, you don't have to be exact. It's very easy to make them bigger or smaller, and to put them in a different spot on the form.

Size controls with the mouse

1. In the Toolbox, select the Pointer tool (if it isn't already selected).

2. On your form, select the control you want to resize.

3. Grab a sizing handle with the mouse by moving the pointer over it and then holding down the left mouse button. You know when you're over the sizing handle because the mouse pointer turns into a double-sided arrow.

4. While holding down the mouse button, notice that a box appears (see Figure 4.5). The box shows you what the size of the control will be. When it's the right size, release the mouse button.

FIGURE 4.5 You can resize a control by dragging a sizing handle.

Changing the position of a control is also easy. Just click it to select it, and drag it to its new position.

Modifying a Form's Size

Every new form you start using in Visual Basic will be the same size. However, this size probably won't be right for your program, so you'll have to resize it to better fit the controls you're using. You resize a form just as you resize a control: Grab one of its sizing handles with the mouse and drag it to the proper size (see Figure 4.6).

FIGURE 4.6 Resize your forms to better fit the controls you're using.

Working with Containers

A container is a control that can hold other controls within it, such as a Frame or a PictureBox--or a form. Controls inside a container are child controls. Child controls exist completely within their containers. This means that they can't be moved outside their containers (see Figure 4.7) and that when a container is deleted, so are any child controls within it.

FIGURE 4.7 If you try to move a child control outside its parent, part of the control will be cut off.

You can add a control to a container by using the drawing method. To do so, you must select the container on your form before you click the control in the Toolbox. Then draw the control inside the container.

The concept of container controls becomes especially important when you move and resize controls through code. For now, just remember that some controls can contain other controls and that any controls you put on a form are contained within the form.

Extending Visual Basic by Adding Controls

Although the intrinsic controls are powerful, they won't suit all your programming needs. Luckily, Visual Basic ships with many other ActiveX controls for you to use in your programs--they just aren't shown in the Toolbox by default. You have to add them.

Add controls to the Toolbox

1. From the Project menu, select Components.

2. In the Components dialog, make sure that the Selected Items Only check box is deselected on the Controls page. You then see a list of all ActiveX controls available on your computer.

3. From the list, select the control you want to add by checking its check box (see Figure 4.8).

FIGURE 4.8 You can add ActiveX controls to the Toolbox by checking boxes in the Components list.

4. Click OK. The control you selected will now appear in the Toolbox, and you can use it just as you use the intrinsic controls (see Figure 4.9).

FIGURE 4.9 After adding the calendar control, you can access it from the toolbox.


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.