Event Binding for Visual FoxPro Objects

You can use event binding to trigger events, properties, or methods of native Visual FoxPro objects from other Visual FoxPro objects using the following functions:

  • BINDEVENT( )

    Associates an event from a native Visual FoxPro object with the method or event of another Visual FoxPro object.

  • UNBINDEVENTS( )

    Detaches events previously bound to Visual FoxPro objects.

  • RAISEEVENT( )

    Raises, or triggers, events for user-defined custom methods. You can also use RAISEEVENT( ) for native events and methods.

  • AEVENTS( )

    Retrieves information about the number of existing event bindings.

For a solution sample demonstrating binding events, raising events programmatically, unbinding events, and retrieving events, see Binding, Raising, Unbinding, and Retrieving Events Sample.

For information about binding events from Component Object Model (COM) objects, see Event Binding for COM Objects and EVENTHANDLER( ) Function.

Binding an Event

You can use the BINDEVENT( ) function to attach, or bind, an event, method, or property from one Visual FoxPro object, or event source, to the method or event of another Visual FoxPro object, or the event handler. The method that handles the event acts as a "delegate" for the event handler.

You can bind to any valid Visual FoxPro object method or event, including the Access and Assign methods. However, both the event source and event handler must be valid Visual FoxPro objects. You cannot use COM objects or bind to methods of objects in collections referenced by the _VFP system variable because these collections also pass through COM.

Note

When you specify objects for event binding, make sure they are fully instantiated. Do not bind events, methods, or properties to an object in its Load or Init event because the object might not yet be fully instantiated and can cause the binding operation to fail.

You can use the same object as both the event source and event handler. The following example shows how Form1 acts as both source and handler:

BINDEVENT( Form1, "Resize", Form1, "myresize1" )

You can use different objects from the same class as the event source and handler. However, the event of an object that is used as both the event source and handler cannot be the same as the delegate method.

You can bind multiple event handlers to the same event source and event in an act referred to as "multi-casting." If multiple handlers exist for the same event, events occur in first in, first out (FIFO) order.

You can bind multiple delegate methods from an event handler to a particular event source and event. For example:

BINDEVENT( Form1, "Resize", oHandler, "myresize1" )
BINDEVENT( Form1, "Resize", oHandler, "myresize2" )

You cannot bind to an event with parameters that are passed by reference. Though calling BINDEVENT( ) succeeds, raising the event, for example, using RAISEEVENT( ), fails.

Visual FoxPro does not support event binding in designers such as the form and class designers, even though you can obtain object references using the ASELOBJ( ) or SYS(1270) functions.

For more information, see BINDEVENT( ) Function, ASELOBJ( ) Function, and SYS(1270) - Object Location. For a sample, see Bind, Raise, Unbind, and Retrieve Events Sample.

Unbinding an Event

You can use the UNBINDEVENTS( ) function to detach events, methods, and properties that were bound using the BINDEVENT( ) function from native Visual FoxPro objects. UNBINDEVENTS( ) returns the number of events that it unbinds if it succeeds. You can specify a Visual FoxPro object event source or an object reference, which can be used as the event source or event handler.

For more information, see UNBINDEVENTS( ) Function.

Raising an Event

You can use the RAISEEVENT( ) function to raise, or trigger, events for custom and native methods. Directly calling methods does not make events occur, unless you set the appropriate flags when using BINDEVENT( ) to attach events to other objects. Therefore, you need RAISEEVENT( ) to trigger these events.

For more information, see RAISEEVENT( ) Function.

Retrieving Event Binding Information

You can use the AEVENTS( ) function to retrieve the number of Visual FoxPro events that are currently bound to Visual FoxPro objects. Visual FoxPro stores the information in an array. Depending on the parameters you pass, AEVENTS( ) returns either a single-element array containing an object reference to the event source or an array which represents each existing event binding as a row and contains information about the binding across five columns.

For more information, see AEVENTS( ) Function.

See Also

Other Resources

Event Binding for Objects

Understanding the Event Model