Spin Button events

To help clarify the concept of events, this section takes a close look at the events associated with a

SpinButton control.

CD- The companion CD-ROM contains a workbook that demonstrates the sequence of events ROM that occur for a SpinButton and the UserForm that contains it. The workbook, named ®

spinbutton events. xlsm, contains a series of event-handler routines - one for each SpinButton and UserForm event. Each of these routines simply displays a message box that tells you the event that just fired.

Table 13-1 lists all the events for the SpinButton control.

Table 13-1: SPINBUTTON EVENTS

^ Open table as spreadsheet

Table 13-1: SPINBUTTON EVENTS

^ Open table as spreadsheet

Event

Description

AfterUpdate

Occurs after the control is changed through the user interface.

BeforeDragOver

Occurs when a drag-and-drop operation is in progress.

BeforeDropOrPaste

Occurs when the user is about to drop or paste data onto the control.

BeforeUpdate

Occurs before the control is changed.

Event

Description

Change

Occurs when the Value property changes.

Enter

Occurs before the control actually receives the focus from a control on the same UserForm.

Error

Occurs when the control detects an error and cannot return the error information to a calling program.

Exit

Occurs immediately before a control loses the focus to another control on the same form.

KeyDown

Occurs when the user presses a key and the object has the focus.

KeyPress

Occurs when the user presses any key that produces a typeable character.

KeyUp

Occurs when the user releases a key and the object has the focus.

SpinDown

Occurs when the user clicks the lower (or left) SpinButton arrow.

SpinUp

Occurs when the user clicks the upper (or right) SpinButton arrow.

A user can operate a SpinButton control by clicking it with the mouse or (if the control has the focus) by using the up-arrow and down-arrow keys.

MOUSE-INITIATED EVENTS

When the user clicks the upper SpinButton arrow, the following events occur in this precise order:

1. Enter (triggered only if the SpinButton did not already have the focus)

2. Change

3. SpinUp

KEYBOARD-INITIATED EVENTS

The user can also press Tab to set the focus to the SpinButton and then use the arrow keys to increment or decrement the control. If so, the following events occur (in this order):

1. Enter

2. KeyDown

3. Change

4. SpinUp (or SpinDown)

5. KeyUp

WHAT ABOUT CHANGES VIA CODE?

The SpinButton control can also be changed by VBA code - which also triggers the appropriate event(s). For example, the following statement sets the SpinButton1 Value property to 0 and also triggers the Change event for the SpinButton control - but only if the SpinButton value was not already

SpinButton1.Value = 0

You might think that you could disable events by setting the EnableEvents property of the

Application object to False. Unfortunately, this property applies only to events that involve true Excel objects: Workbooks, Worksheets, and Charts.

+2 -4

Responses

Post a comment