Provides ability to execute user code (an object method) when an event occurs.
With the first form of the syntax below, you can use BINDEVENT(В ) to bind events, properties, or methods from native Visual FoxPro objects to other Visual FoxPro objects.
With the second syntax variation below, you can bind to Windows Message (Win Msg) events.
|If you want to bind to events from Component Object Model (COM) objects, use the EVENTHANDLER(В ) function.|
BINDEVENT(oEventSource, cEvent, oEventHandler, cDelegate [, nFlags])
BINDEVENT(hWnd | 0, nMessage, oEventHandler, cDelegate [, nFlags])
- Specifies the event source, which must be a valid Visual FoxPro object.
- Specifies the integer handle of the window that receives Windows Messages. If a value of 0 is passed, the specified event (nMessage) is trapped for all windows. You can use the hWnd Property (Visual FoxPro) to bind Windows Messages (events) received by _VFP, _SCREEN and instantiated form. ActiveX controls also have an hWnd property
- Specifies the name of the event, method, or property you want to bind.
- Specifies a valid Windows message that is trapped. See MSDN (the Microsoft Developer Network) for information about Windows messages.
- Specifies the object, which must be a valid Visual FoxPro object, handling the event.
- Specifies the method, or "delegate", that handles the event for oEventHandler. The delegate method must have the same parameters as the event specified in cEvent. You can call the AEVENTS(В ) function to retrieve an object reference to the event source. If the delegate method does not have enough parameters to handle those passed by the event, Visual FoxPro generates an error. When trapping for Windows Message (Win Msg) events, the cDelegate method must include a PARAMETERS statement to accept four parameters that get passed to the method. The format of the parameters is identical to the format of the Windows WindowProc function. See MSDN (the Microsoft Developer Network) for information about the Windows WindowProc function. The method must return an integer value.
Specifies an additive bit flag you can set for the event binding operation.
The nFlags parameter is ignored when a Windows message event binding is created.
1If you use an nFlags value of 1, the value returned by a method call to the event is not that of the event, but rather that of the last delegate invoked. The event gets called before any of the delegates, so the return value of the delegate is the remaining value on the stack. Therefore, it is recommended that the delegate method contain the same return value as that of the event itself. In Visual FoxPro, a procedure that has no explicit RETURN statement returns an implicit value of True (.T.). This is an issue only if the event was triggered by method call and not via normal interactive mode or RAISEEVENT(В ) call. The following table shows whether an event is raised when Bit 1 is off or on.
nFlags Bits Event Type Description
Call delegate code before event code. (Default)
Call event code before delegate code.
Do not trigger event (call delegate code) by simple method call.
Call event code before delegate code. Do not trigger event (call delegate code) when simple method calls occur.
Prevents recursion of similar events while user event code is executing.
Event trigger OFF (Default) ON
Numeric data type. BINDEVENT(В ) returns the number of bindings for the object's event.
BINDEVENT(В ) always returns 1 when a Windows message event binding is created. No error detection is performed, so if invalid hWnd and nMessage values are specified, 1 is still returned and the binding remains in effect until it is released.
You can bind to any valid Visual FoxPro object event, property, or method, including the Access and Assign methods. However, the event and delegate methods must be public, not protected or hidden, members of the class.
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.
When you bind to a property, you should bind to it directly and not to the Assign method. If you bind directly to the Assign method, be aware that Access and Assign methods are marked as Protected and are not visible except within the class.
|If you bind to a property that has an Assign method, the delegate method might trigger twice. The first time is when the property assignment call is made. The second time is when the property is actually set, within the Assign method, to the parameter that is passed. The delegate method should be aware of this possibility.|
Normal rules of inheritance apply. If the delegate method does not contain any code, Visual FoxPro traverses up the parent hierarchy.
An event handler is called when an event occurs or if it is called as a method. Calling the event as a method triggers the event unless you specify an nFlags value of 2 or 3.
By default, Visual FoxPro calls the delegate method before the event. However, you can change the default behavior by using an nFlags setting.
If you specify a property as the event you want to bind, Visual FoxPro binds that property to an implicit Assign method. When the value of that property changes, Visual FoxPro triggers an event.
If an invalid parameter is passed, Visual FoxPro generates the error, "Function argument value, type, or count is invalid." However, if a problem occurs during the binding operation, Visual FoxPro does not generate an error. You can retrieve the return value of BINDEVENT(В ) to check the number of bindings.
Certain control events such as GotFocus, LostFocus, InteractiveChange, and ProgrammaticChange do not work if the second bit of the nFlags parameter is set, for example, nFlags set to 2. These events are treated as method calls internally by Visual FoxPro, even though they are considered events. The same behavior applies to the Refresh method of an object on a form that is called when the form's Refresh method is called. Certain events such as When and Valid require code in the event for it to occur.
BINDEVENT(В ) does not directly support the Value property because it is handled by Visual FoxPro in a special way. You should use the InteractiveChange and ProgrammaticChange events instead. Additionally, the ActivePage property is not supported.
If the original event contains a NODEFAULT command, Visual FoxPro still processes the event because it is possible that the delegate method is called before the event. NODEFAULT applies only to native Visual FoxPro events.
If you make an exact duplicate BINDEVENT(В ) call, Visual FoxPro disregards the call but still returns the number of bindings for the object's event. If you change the nFlags setting, you can call BINDEVENT(В ) to rebind the event.
When binding to Windows message (Win Msg) events, only one hWnd to Windows message pairing can exist. You can pass an hWnd value of 0 if you want to bind all windows to the same Windows message event. A Windows message event binding can be released with the UNBINDEVENTS( ) Function and the CLEAR Commands. Also, if the event handler object specified with the oEventHandler parameter no longer exists, the binding is released when its Windows message occurs.
With a Windows message event binding, your user code will execute whenever an event occurs including scenarios in which a modal dialog is displayed. This is because the Window Procedure must always process the message and return. Since it is possible for recursion to occur with an event while your user code is executing, you may want to specify an nFlags value of 4 to prevent this from happening.
|A Windows message event binding should be used with care since your user code is bound to events being triggered by the Windows operating system and these events can occur at times when you do not expect them to.|
The following example shows how you can keep the Class Browser positioned to the right side of the Visual FoxPro desktop, regardless of how the desktop is resized. BINDEVENT(В ) associates the Resize event of the _SCREEN system variable, or Visual FoxPro desktop, with
oHandler, which uses
myresize as its delegate. The code for
myresize runs when the Resize event is triggered.
PUBLIC oHandler oHandler=NEWOBJECT("myhandler") DO (_browser) BINDEVENT(_SCREEN,"Resize",oHandler,"myresize") DEFINE CLASS myhandler AS Session PROCEDURE myresize IF ISNULL(_obrowser) THEN UNBINDEVENTS(THIS) ELSE _obrowser.left = _SCREEN.Width - _obrowser.width ENDIF RETURN ENDDEFINE