Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Controls Provide the Interface

The controls you select for your application's form are important because the controls (also called tools) provide the application interface for your users. Users interact with your application by clicking the controls and entering text in the controls. Placing and sizing controls are perhaps the two most important tasks you can master at this point.

Placing Controls

After you increase the Form window to a reasonable size, your job is to place controls on the form. Use either of these two methods for placing controls on the form:

Sizing and Moving Controls

You can change the size of only a selected control. The eight sizing handles are the key to resizing the control. You can drag any of the eight sizing handles in any direction to increase or decrease the control's size. Of course, if you placed a control on the form by dragging the control, you won't need to resize the control as often as you would if you double-clicked the toolbox tool to place the control.

You can move a selected control to any area of the Form window by dragging the control with your mouse. After you click to select a control, click the control and hold down the mouse button to drag the control to another part of the Form window.

Sometimes you might want to drag several controls to a new location as a group. For example, perhaps you've placed a set of command buttons at the bottom of a form and after adjusting the Form window's size, you determine that you need to move the buttons down some. Although you can move the command buttons one at a time, you can more quickly select all the command buttons and move them as a group.

In addition, you can lasso the controls by dragging a selection rectangle around the controls you want to select as a group. When you release your mouse, the controls within the selected region will be selected, like those shown in Figure 3.3.

03fig03.jpg

Figure 3.3 Selecting multiple controls when you want to move the entire group at once.

Setting Properties

As you add controls to the Form window, the Properties window updates to show the properties for the currently selected control. The selected control is usually the control you last placed on the form. Visual Basic lets you see a control's properties in the Properties window by clicking to select the control or by selecting the control from the Properties window's drop-down list box, as shown in Figure 3.4.

03fig04.gif

Figure 3.4 Selecting the control to work with.

The Left, Top, Height, and Width properties are about the only properties you can set without accessing the Properties window. As you size and move a control into place, Visual Basic updates the Left, Top, Height, and Width properties according to the control's placement on the Form window and the control's size. As with the form location and size measurements, these properties appear in twips (unless you specify a different value in the ScaleMode property). Left indicates how far from the form's left edge the control appears, Top indicates how far from the top of the form the control appears, and the Height and Width properties indicate the control's size.

After you place and size a control, the first property you should modify is the Name property. Although Visual Basic assigns default names to controls when you place them on the Form window, the default names don't indicate the control's true purpose in your application. In addition, the default names don't contain the three-letter prefix that describes the control.

For your reference, Table 3.1 lists common prefixes used for control names. When you name your Form window's controls, you'll appreciate later that you took the time to type the three-letter abbreviations at the beginning of the names. Then you'll be less likely to assign to a text box, a property that belongs to a command button control inside an event procedure. (Such an assignment will cause a runtime error.)

Table 3.1. Use these prefix abbreviations for control names.

Prefix Control
cbo Combo box
chk Check box
cmd Command button
dir Directory list box
drv Drive list box
fil File list box
fra Frame
frm Form
grd Grid
hsb Horizontal scrollbar
img Image
lbl Label
lin Line
lst List box
mnu Menu
ole OLE client
opt Option button
pic Picture box
shp Shape
tmr Timer
txt Text box
vsb Vertical scrollbar

A ToolTip is a pop-up description box that appears when the user rests the mouse pointer over a control.

Some property values you set by typing the values directly in the Properties window. For example, to enter a value for a control's ToolTipText property, click once on the Properties window's ToolTipText property and type the ToolTip text.

Share ThisShare This

Informit Network