Events in Visual FoxPro

Event code is triggered automatically by the system in response to some user action. For example, code written for the Click event is automatically processed by the system when the user clicks on a control. Event code can also be triggered by system events, as in the case of the Timer event in a timer control.

The Core Events

The following table contains a list of the core set of Visual FoxPro events, which apply to most controls.

Core Event Set

Event When the event is triggered
Init An object is created.
Destroy An object is released from memory.
Click The user clicks the object using the primary mouse button.
DblClick The user double-clicks the object using the primary mouse button.
RightClick The user clicks the object using the secondary mouse button.
GotFocus The object receives the focus, either by user action such as tabbing or clicking, or by changing the focus in code using the SetFocus method.
LostFocus The object loses the focus, either by user action such as tabbing to or clicking another object, or by changing the focus in code using the SetFocus method.
KeyPress The user presses and releases a key.
MouseDown The user presses the mouse button while the mouse pointer is over the object.
MouseMove The user moves the mouse over the object.
MouseUp The user releases the mouse button while the mouse pointer is over the object.

Containers and Object Events

There are two basic rules to keep in mind when you are writing event code for controls:

  • Containers do not process events associated with the controls they contain.
  • If no event code is associated with a control, Visual FoxPro checks to see if there is code associated with the event higher up the class hierarchy for the control.

When a user interacts with an object in any way — by tabbing to it, clicking it, moving the mouse pointer over it, and so on — object events take place. Each object receives its events independently. For example, even though a command button is on a form, the form's Click event is not triggered when a user clicks the command button; only the command button's Click event is triggered.

If there is no Click event code associated with the command button, nothing happens when the user clicks the button, even though there is Click event code associated with the form.

This rule is also true of grid controls. The grid contains columns, which in turn contain headers and controls. When events occur, only the innermost object involved in the event recognizes the event. The higher-level containers do not recognize the event.

There is an exception to this rule, however. If you have written event code for an option button group or a command button group but there is no code for the event in a particular button in the group, the group event code is executed when the button event occurs.

Note   When a sequence of events, such as MouseDown then MouseUp, is initiated for a control, the whole sequence of events belongs to the control. For example, if you click the left mouse button on a command button and drag the mouse pointer away from the command button, the command button's MouseMove events continue to occur, even though the mouse pointer is moving over the form. If you release the left mouse button over the form instead of over the command button, the MouseUp event that occurs is associated with the command button rather than the form.

Classes and Control Events

If a control on a form is based on a user-defined class (which, in turn, could be based on another user-defined class), Visual FoxPro checks in the immediate control for event code when an event occurs. If there is code in that event procedure, Visual FoxPro executes it. If no code exists in the event procedure, Visual FoxPro checks the next level up in the class hierarchy. If at any point in the class hierarchy Visual FoxPro finds code for the event, that code is executed. Any code further up the hierarchy is not executed.

You can, however, include code in an event procedure and explicitly call the code in classes that the control is based on by using the DODEFAULT( ) function.

See Also

Understanding the Event Model | Tracking Event Sequences | Assigning Code to Events | SetFocus method