Learn the most popular programming language of the most popular Operating System: Windows

Ad

Coding Event Procedure for Control

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.

Table 3.1 The 12 Controls
ControlDefault NameName Prefix
Text BoxText1txt
LabelLabel1 txt
List BoxList1lst
Combo Boxcombo1cbo
Picture Boxpicture1pic
FrameFrame1fra
Option Buttonoption1opt
Command Buttoncommand1cmd
Check Boxcheck1chk
Image Image1img
Horizontal Scroll BarHScroll1hsb
Vertical Scroll BarVScroll1vsb

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.

Figure 3.4

In this display, the user is expected to click the Start button to display the message on the form.

  1. Begin a new project or continue the last.
  2. Add a command button to the form.
  3. With the form active (not the command button), press F4 to reveal the properties box.  Make the following change:

    Caption = A Printed Message

  4. With the command button active, press F4 to reveal the properties box. Make the following changes:

    Caption = Start

    Name = cmdStart

  5. 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
  6. Save the project.
  7. Close the Code window and run the program.  Click the Start button.
  8. 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.
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts