Partilhar via


BINDEVENT( ) Function

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.

Note

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])

Parameters

  • oEventSource
    Specifies the event source, which must be a valid Visual FoxPro object.
  • hWnd
    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

  • cEvent
    Specifies the name of the event, method, or property you want to bind.
  • nMessage
    Specifies a valid Windows message that is trapped. See MSDN (the Microsoft Developer Network) for information about Windows messages.
  • oEventHandler
    Specifies the object, which must be a valid Visual FoxPro object, handling the event.
  • cDelegate
    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.

  • nFlags
    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.

    nFlags Bits Event Type Description

    0

    000

    FoxPro object

    Call delegate code before event code. (Default)

    1

    001

    FoxPro object

    Call event code before delegate code.

    2

    010

    FoxPro object

    Do not trigger event (call delegate code) by simple method call.

    3

    011

    FoxPro object

    Call event code before delegate code. Do not trigger event (call delegate code) when simple method calls occur.

    4

    100

    Windows Message

    Prevents recursion of similar events while user event code is executing.

    The following table shows whether an event is raised when Bit 1 is off or on.

    Event trigger OFF (Default) ON

    Interactive

    YES

    YES

    Programmatic

    YES

    NO

    RAISEEVENT( )

    YES

    YES

Return Value

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.

Remarks

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.

Note

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.

Note

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.

Example

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

See Also

Tasks

Bind, Raise, Unbind, and Retrieve Events Sample

Reference

AEVENTS( ) Function
RAISEEVENT( ) Function
UNBINDEVENTS( ) Function
SYS(2325) - WCLIENTWINDOW from Visual FoxPro WHANDLE
SYS(2326) - WHANDLE from a Window's hWnd
SYS(2327) - Window's hWnd from Visual FoxPro WHANDLE

Concepts

Event Binding for Visual FoxPro Objects

Other Resources

Functions