Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Event Procedures

Visual Basic makes it easy to locate event procedure code for controls on forms. Double-click any control to see one of its event procedures. For example, if you double-click the Exit command button, Visual Basic opens the Code window and places the text cursor in the set of lines shown in Listing 2.1.

Example 2.1. The Exit command button's Click event procedure.

1: Private Sub cmdExit_Click()
2: ' Unload the form and terminate application
3: Unload frmInterest
4: End
5: End Sub

Wrapper lines are the first and last lines of a procedure.

A block is a section of code that goes together as a single unit.

Don't sweat the details, but become familiar with the overall event procedure. Most event procedures begin with the statement Private Sub… and end with End Sub. The Private…End block illustrates the first and last lines of the event procedure. The lines between these wrapper lines compose the body of the event procedure.

All controls have unique names as you saw earlier. All event procedures also have unique names. An event procedure name always takes this form:


   controlName_eventName ()

The event procedure always consists of the control name, an underscore, and the procedure's event name. If you want to respond to both the click and double-click events that might be applied to the Exit command button, you would have to write an event procedure named cmdExit_Click() and one named cmdExit_DblClick().

You don't have to memorize that the double-click event is named DblClick and that a keypress event is named KeyDown. The top of every Code window contains a drop-down list box, which contains every event possible for the control listed in the left-hand drop-down list box. The left-hand list box holds the name of every control on the form. Again, don't get too bogged down in details because when it's time to use these drop-down list boxes to select events, this lesson describes the process in detail.

The naming convention for the event procedure isn't your decision, but Visual Basic's. The Click event procedure for a command button named cmdTest will always have to be cmdTest_Click(). The two-part name makes the event procedure extremely specific; from the name, both you and Visual Basic know that the code executes only if the user clicks the cmdTest command button.

Share ThisShare This

Informit Network