Share via


Tracking Event Sequences

The Visual FoxPro event model is extensive, allowing you a great deal of control over the components of your application in response to a wide variety of user actions. Some of the event sequences are fixed, as for example, when a form is created or destroyed. Some events occur independently, but most occur in conjunction with several other events based on user interaction.

Setting Event Tracking On

The best way to see the Visual FoxPro event sequences is to set event tracking on in the debugger. Event tracking allows you to see when each event associated with your own forms and controls occurs in relation to other events, so that you can determine the most efficient place to include your code.

To set event tracking on

  1. From the Tools menu in the Debugger window, choose Event Tracking.
  2. In the Event Tracking dialog box, select Turn event tracking on.

The events in the Events to track list are written to the Debugger Output window or a file as they occur.

Note   In this example, the MouseMove and Paint events have been removed from the Events to track list because these events occur so frequently that they make it more difficult to see the sequences of the other events.

Watching Events Occur

Sometimes a single event is triggered by a user action, such as the user moving the mouse pointer over a control. Often, however, a user action triggers multiple events.

This section describes the order in which events occur in response to user interaction, using the following form as an example.

A sample form to illustrate event sequences

In this example scenario, the user performs the following actions on the form:

  1. Runs the form.
  2. Types text in Text1.
  3. Selects the field, and copies it to the clipboard.
  4. Moves to Text2.
  5. Pastes the text into Text2.
  6. Closes the form by clicking Command2.

These actions trigger one or more system events for each object. The following tables detail the events triggered in response to each user action.

Action 1

The user runs the form by typing the following command in the Command window:

DO FORM form1 NAME frmObject

Visual FoxPro loads the form, initializes each object, then initializes the form; the form is activated and then the first field receives input focus.

Object Event
DataEnvironment BeforeOpenTables
Form1 Load
DataEnvironment Init
Text1 Init
Text2 Init
Command1 Init
Command2 Init
Form1 Init
Form1 Activate
Form1 GotFocus
Text1 When
Text1 GotFocus

Action 2

The user types Test in Text1. Each keystroke generates two events. The KeyPress event receives 2 parameters: the pressed key and the state of the SHIFT, ALT, and CTRL keys.

Object Event
Text1 KeyPress(84, 1) "T"
Text1 InteractiveChange
Text1 KeyPress(101, 0) "e"
Text1 InteractiveChange
Text1 KeyPress(115,0) "s"
Text1 InteractiveChange
Text1 KeyPress(116,0) "t"
Text1 InteractiveChange

Action 3

The user double-clicks Text1 to select the text, then presses CTRL+C to copy the text to the Clipboard. Mouse events and a Click event accompany the DblClick event. The MouseMove and MouseDown events receive four parameters: a number indicating which button was pressed, the Shift state, and X and Y locations. The X and Y locations are relative to the form and reflect the scale mode (for example, pixels) of the form. Only one MouseMove event is listed for each control. In actuality, this event would probably fire half a dozen times or more.

Object Event
Form1 MouseMove(0, 0, 100, 35)
Text1 MouseMove(0,0,44,22)
Text1 MouseDown(1, 0, 44, 22)
Text1 MouseUp(1, 0, 44, 22)
Text1 Click
Text1 MouseDown(1, 0, 44, 22)
Text1 MouseUp(1, 0, 44, 22)
Text1 DblClick

Action 4

The user presses TAB to move to Text2.

Object Event
Text1 KeyPress(9, 0)
Text1 Valid
Text1 LostFocus
Text2 When
Text2 GotFocus

Action 5

The user pastes the copied text into Text2 by pressing CTRL+V.

Object Event
Text2 InteractiveChange

Action 6

The user clicks Command2, which closes the form.

Object Event
Form1 MouseMove
Command2 MouseMove
Text2 Valid
Command2 When
Text2 LostFocus
Command2 GotFocus
Command2 MouseDown(1, 0, 143, 128)
Command2 MouseUp(1, 0, 143, 128)
Command2 Click
Command2 Valid
Command2 When

As the form closes and the object is released, these additional events take place, in opposite order to the events in Action 1.

Object Event
Form1 Destroy
Command2 Destroy
Command1 Destroy
Text2 Destroy
Text1 Destroy
Form1 Unload
DataEnvironment AfterCloseTables
DataEnvironment Destroy

The Visual FoxPro Event Sequence

The following table shows the general firing sequence of Visual FoxPro events. The data environment's AutoOpenTables property is assumed to be set to true (.T.). Other events can occur based on user interaction and system response.

Object Events
Data environment BeforeOpenTables
Form set Load
Form Load
Data environment cursor(s) Init
Data environment Init
Objects 1 Init
Form Init
Form set Init
Form set Activate
Form Activate
Object1 2 When
Form GotFocus
Object1 GotFocus
Object1 Message
Object1 Valid3
Object1 LostFocus
Object2 3 When
Object2 GotFocus
Object2 Message
Object2 Valid4
Object2 LostFocus
Form QueryUnload
Form Destroy
Object 5 Destroy
Form Unload
Form set Unload
Data environment AfterCloseTables
Data environment Destroy
Data environment cursor(s) Destroy

1. For each object, from innermost object to outermost container
2. First object in the tab order
3. Next object to get focus
4. As the object loses focus
5.For each object, from outermost container to innermost object

See Also

Events in Visual FoxPro | Assigning Code to Events | Debugger | Command | KeyPress event | Click event | DblClick event | MouseMove | MouseDown | AutoOpenTables property | Understanding the Event Model