Understanding Form and Control Events

In addition to having properties and methods, each form and control has a predefined set of events. An event is some type of action, such as clicking a mouse button, pressing a key, selecting an item from a list, or changing a list of items available in a list box. Events can be triggered by the user or the system. To specify how a form or control should respond to events, write event procedures.

When you design a custom form, you should anticipate and program events that can occur at run time (while the form is being used). The most popular event is the Click event. Every time a command button is clicked, it triggers the appropriate event procedure to respond to the Click event for that button. A form itself can respond to more than 20 separate events, including Click, DblClick, Activate, Initialize, and Resize.

Table 10-2 lists events that are recognized by various form controls. If a control does not recognize a specific event, the table cell displays "N"; otherwise, it is blank. Take a few minutes now to familiarize yourself with the names of the events. For example, take a look at the AddControl event in the table. You can see at a glance that this event is only available for three objects: Frame, MultiPage control, and the UserForm itself.

Table 10-2: Form and control events

Event Name

UserForm

Label

TextBox

ComboBox

ListBox

CheckBox

OptionButton

ToggleButton

Frame

CommandButton

TabStrip Control

MultiPage Control

ScrollBar

SpinButton

Image

RefEdit

Activate

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

AddControl

N

N

N

N

N

N

N

N

N

N

N

N

N

AfterUpdate

N

N

N

N

N

N

N

BeforeDragOver

BeforeDropOrPaste

Before Update

N

N

N

N

N

N

N

Change

N

N

N

N

N

Click

N

N

N

N

DblClick

N

N

Deactivate

N

N

N

N

N

N

N

N

N

N

N

N

N

N

DropButtonClick

N

N

N

N

N

N

N

N

N

N

N

N

N

Enter

N

N

N

Error

Initialize

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

Exit

N

N

N

KeyDown

N

N

KeyPress

N

N

KeyUp

N

N

Layout

N

N

N

N

N

N

N

N

N

N

N

N

N

MouseDown

N

N

MouseMove

N

N

MouseUp

N

N

QueryClose

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

RemoveControl

N

N

N

N

N

N

N

N

N

N

N

N

N

Resize

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

Scroll

N

N

N

N

N

N

N

N

N

N

N

N

SpinDown

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

SpinUp

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

Terminate

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

Each form that you create contains a form module for storing VBA event procedures. To access the form module to write an event procedure or to find out the events recognized by a specific control, you can:

■ Double-click a control

■ Right-click the control, and choose View Code from the shortcut menu

■ Click the View Code button in the Project Explorer window

■ Double-click any unused area of the UserForm

Executing any of the above actions results in opening the code window for the form. Figure 10-15 displays the Code window activated by double-clicking a command button placed on a form. Notice the title in the Microsoft Visual Basic title bar: Chap10.xls (UserForm1(Code)). A form module contains a general section as well as individual sections for each control placed on the form. The general section is used for declaration of form variables or constants.

You can access the desired section by clicking the down arrow to the right of the combo box at the upper right-hand side. This combo box is called the Procedure box. It displays the event procedures that are recognized by the control selected in the left-hand side combo box. Events that already have procedures written for them appear in bold.

Microsoft Visual Basic - Chap10.xls - [UseiFoiml (Code)]

4} File Edit View (risal Farns Debug Run Joote Add-lrvs Window Help VBAcodePrmt - S X

<j tj : J If: • • • £ ~i a L « ¡ife ® © ~ *L &! 1*1

Pioiect - CustomFmms

El ^ CustomForms (ChapIO.xls)

i g Microsoft Excel Objects ®P| Sheet! (Info Survey) @ Shest2 (SheetZ) if) Sheet? (Sheet3) This Workbook El Forms j-H InfoSurvey

| UserForml User Form ' I

Alphabetic | Categorized j

(Name)

| UserForml

BackColor

□ SiHSOOOOOOf^

BordefCctor BorderStyte

Private Sub CommandElultort1_Clii End Sub jreDropOrPaste

Private Sub CommandElultort1_Clii End Sub jreDropOrPaste aajlLJ

Figure 10-15: The Procedure box lists the available event procedures for the command button control.

0 0

Post a comment