Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Creating New Applications
- Controls Provide the Interface
- Giving Your Users Help
- Named Literals
- Take a Break!
- Summary
- Q&A
- Workshop
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
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:
-
Double-click any control on the Toolbox window to place that control on the Form window. As Figure 3.1 shows, the control appears in the center of the Form window.
Figure 3.1 The command button appears in the center of the Form window.
If a control appears in the center of the form already, the new control will be on top of the existing control. You can drag the new control to a different location. The eight sizing handles (the small boxes that appear around a selected control) indicate that the control is selected. If several controls appear on the Form window, the selected controls will display their sizing handles. (Typically, only one control will be selected at any one time but you can select multiple controls by holding the Ctrl key and clicking several controls.)
-
If you click a toolbox control once, the toolbox highlights the control. If you then move the mouse cursor to the Form window, the mouse cursor turns into a crosshair indicating that you can place the selected control anywhere on the form. Although a control appears in the center of the Form window automatically as soon as you double-click the control, a selected control appears only when you click and drag your mouse crosshair on the Form window. The final control appears when you release the mouse.
The advantage of using this approach to placing controls over the first approach is that you don't have to move and resize the control after you've placed it. Figure 3.2 shows Figure 3.1's command button placed in the center of the form with a double-click, as well as a new command button placed on the form by dragging the control. You can place the control exactly where you want it and at the size you want it when you drag the control onto the form.
Figure 3.2 The second command button is placed and sized immediately.
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.
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.
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.
Giving Your Users Help | Next Section

Account Sign In
View your cart