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:
Associates an event from a native Visual FoxPro object with the method or event of another Visual FoxPro object.
Detaches events previously bound to Visual FoxPro objects.
Raises, or triggers, events for user-defined custom methods. You can also use RAISEEVENT(В ) for native events and methods.
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.
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.
|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.
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.