Page 297 - Excel Progamming Weekend Crash Course
P. 297
r540629 ch21.qxd 9/2/03 9:35 AM Page 272
272 Sunday Morning
Table 21-1 Continued
Event Available for Occurs
Change CheckBox, OptionButton, When the control’s data changes
ComboBox, TextBox, and some
other controls
Enter Most controls Just before the control receives the
focus
Exit Most controls Just before the control loses the focus
KeyDown TextBox and other controls When a key is pressed while the control
has the focus; the event procedure
receives information identifying the key.
MouseMove Form, controls When the mouse cursor moves over the
object
If you do not place code in the event procedure for an event, that event is
ignored.
Note
Events are connected to your VBA program by event procedures. An event procedure is a
section of code that executes automatically when the event occurs, and it is identified by
two pieces of information:
The name of the object (a control or the form) that receives the event.
The name of the event.
The convention for naming event procedures is objectname_eventname. For example, for
a CommandButton control named cmdOK, the event procedure for the Click event is named
cmdOK_Click. This name is all that is required to connect the procedure to the event.
The interval that is required for a double click (as opposed to two separate
clicks) is set in the Windows Control Panel under the Mouse heading.
Note
The KeyDown event deserves special mention because it can be handy when used in con-
junction with a TextBox control. Specifically, it enables you to intercept keystrokes before
they reach the TextBox, seeing what character was entered and then either accepting it or
discarding it according to the function of the control. For example, you can use this event
to create a TextBox that accepts only numeric input.
The KeyDown event procedure receives two arguments when the event occurs:
KeyCode. An integer value specifying the key that was pressed.
Shift. An integer value identifying which shift keys (Shift, Alt, and Ctrl) were
down when the key was pressed.