Coding an Event Procedure for a Control
Adding a control to a form and setting the control properties set the scene for adding a procedure. Remember that a control will do nothing to an event-oriented environment unless an event procedure is attached to it. To add to this concept, more than one event can be associated with a single control, as with a form. In the next exercise you will examine the various event and alternatives for a single control.
Controlling Naming Conventions
When adding an event procedure, a default name is always assigned to the form or control unless you decide to change it. For example, the first command button added to a form is named Command1, and the second is Command2. In this text, we ask you to follow a command syntax by adding a name prefix to a control (and many times to forms). For example, if you decide to add a command button to a form and change the command caption to Start, you would name the control cmdStart. The cmd is the command prefix. The prefix always tells you what specific control you are working with.
The exception to the above rule is when you are working with labels that have no effect on the input or output to a program. Because changing the name of each control takes considerable time, you should often leave label conventions (Label1, Label2, and so on) alone, unless the content of a label is determined by a procedure. This separates labels whose content is determined by a procedure from those whose content is not determined by a procedure.
Table 3.1 shows the controls, the default names and the name prefixes that are used for the 12 controls.
|Control||Default Name||Name Prefix|
|Horizontal Scroll Bar||HScroll1||hsb|
|Vertical Scroll Bar||VScroll1||vsb|
Exercise 3.2 Adding a Procedure to a Command Button
For this exercise, you write a procedure and attach it to a command button. Figure 3.4 shows how the form will look when you run the program.
In this display, the user is expected to click the Start button to display the message on the form.
- Begin a new project or continue the last.
- Add a command button to the form.
- With the form active (not the command button), press F4 to reveal the properties box. Make the following change:
Caption = A Printed Message
- With the command button active, press F4 to reveal the properties box. Make the following changes:
Caption = Start
Name = cmdStart
- With the command button still active, double-click to open the Code window. Enter the following code, between the Private Sub cmdStart_Click() and the End Sub instructions:Private Sub cmdStart_Click() 'The Start command button is used to display a message Dim message As String Print message$ = "Welcome to Visual Basic" Print Spc(20); message$ ' prints 20 spaces followed by the message End Sub
- Save the project.
- Close the Code window and run the program. Click the Start button.
- Exit and open the Code window to examine the other types of events that can be assigned to the cmdStart control. For example, instead of writing a cmdClick() procedure, you might write a MouseDown() procedure, such that when the user presses the mouse button, the message is displayed. As another alternative, write a MouseUp() procedure.