2009-06-26 | VFP9 Bind Event 用法
类别(数据库学习笔记) | 评论(1) | 阅读(570) | 发表于 18:56

 

Visual FoxPro 9.0 SP2

BINDEVENT( ) Function

See Also  Example

 

Collapse All Expand All   Language Filter: All Language Filter: Multiple Language Filter: Visual Basic Language Filter: C# Language Filter: C++ Language Filter: J# Language Filter: JScript

Visual Basic (Declaration)
Visual Basic (Usage)
C#
C++
J#
JScript

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.

 

Copy Code

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

© Microsoft Corporation. All rights reserved.

 

 

 

 

Visual FoxPro 9.0 SP2

AEVENTS( ) Function

See Also  Example

 

Collapse All Expand All   Language Filter: All Language Filter: Multiple Language Filter: Visual Basic Language Filter: C# Language Filter: C++ Language Filter: J# Language Filter: JScript

Visual Basic (Declaration)
Visual Basic (Usage)
C#
C++
J#
JScript

You can use the AEVENTS( ) function to retrieve the number of existing event bindings.

 

 

AEVENTS( ArrayName [, 0 | 1 | oEventObject ] )

Parameters

ArrayName

Specifies the name of the array that contains the results of AEVENTS( ).

0

Specifies that AEVENTS( ) returns a three-element array containing an object reference to the current event source, the name of the triggered event, and how the event was triggered.

Column

Description

Type

1

Event source

Object reference

2

Event

String

3

Event type, or how event was raised.

0 - System

1 - RAISEEVENT( ) function

2 - Method call

The third array element indicates how an event was triggered. If the event is a property, this value can be 1 or 2. The value is 2 if the property is set or assigned.

1

Specifies that AEVENTS( ) returns a four-column array containing information about Windows Message (Win Msg) events.

The array contains one row for each binding. Bindings are created with the BINDEVENT( ) Function.

The following table describes the contents of each column in the array.

Column

Description

Type

1

hWnd

Integer

2

Window Message

Integer

3

Reference of handler

Object reference

4

Handler delegate

String

oEventObject

Specifies an object reference. If you specify an object reference, AEVENTS( ) returns a five-column array that contains the events raised and the delegate methods for oEventObject. Each row in ArrayName represents a binding. The following table describes the information in each of the five columns.

Column

Description

Type

1

.T. if second element is the event source

.F. if second element is the event handler

Logical

2

Event source if you pass the event handler to oEventObject; otherwise, event handler if you pass the event source to oEventObject.

Object reference

3

Event

String

4

Delegate method

String

5

BINDEVENT( ) flags

Integer

When you pass oEventObject, the number of bindings returned by AEVENTS( ) should equal the number of unbound events returned by the UNBINDEVENTS( ) function when passing only the oEventObject parameter.

Return Value

Numeric. AEVENTS( ) returns the number of rows in the specified array. Typically, the number represents the number of event bindings. However, if you pass zero (0), then AEVENTS( ) returns 3.

Remarks

If AEVENTS( ) returns 0, and the array does not exist, Visual FoxPro does not create the array. Visual FoxPro changes or alters an existing array only if valid results are returned. The array remains unchanged under the following conditions:

  • No events exist.
  • You specify a value of 0 as the second parameter, and AEVENTS( ) does not appear within an event or oEventObject has no bindings.

Example

The following example shows how to use AEVENTS( ) to retrieve the number of existing event bindings. BINDEVENTS( ) keeps the Class Browser positioned to the right side of the Visual FoxPro desktop, regardless of how the desktop is resized. For more information, see BINDEVENT( ) Function.

UNBINDEVENTS( ) detaches the Resize event of the _SCREEN system variable from the object, oHandler. AEVENTS( ) returns the number of event bindings before BINDEVENT( ) and after UNBINDEVENTS( ) in an array called myArray:

 

Copy Code

PUBLIC oHandler

 

oHandler=NEWOBJECT("myhandler")

 

DO (_browser)

 

 

 

BINDEVENT(_SCREEN,"Resize",oHandler,"myresize")

 

AEVENTS(myArray,oHandler)

 

numRows=ALEN(myArray,1)

 

numCols=ALEN(myArray,2)

 

WAIT "Rows in array after binding: " + TRANSFORM(numRows) WINDOW AT 20,20

 

WAIT "Cols in array after binding: " + TRANSFORM(numCols) WINDOW AT 20,20

 

FOR count1 = 1 TO numRows

 

   FOR count2 = 1 TO numCols

 

      WAIT myArray[count1,count2] WINDOW AT 20,20

 

   ENDFOR

 

ENDFOR

 

 

 

* Comment the following line to see event binding persist.

 

UNBINDEVENTS(_SCREEN,"Resize",oHandler,"myresize")

 

AEVENTS(myArray,oHandler)

 

 

 

* Check if AEVENTS( ) created and populated new array.

 

IF VARTYPE(myArray) <> U

 

   * The following code does not execute if event is unbound because

 

   * AEVENT( ) does not create a new array if it returns 0. To execute

 

   * the following code and see the contents of the new array, uncomment

 

   * the UNBINDEVENTS( ) statement.

 

   numRows=ALEN(myArray,1)

 

   numCols=ALEN(myArray,2)

 

   WAIT "Rows in array after unbinding: " + TRANSFORM(numRows) ;

 

      WINDOW AT 20,20

 

   WAIT "Cols in array after unbinding: " + TRANSFORM(numCols) ;

 

      WINDOW AT 20,20

 

   FOR count1 = 1 TO numRows

 

      FOR count2 = 1 TO numCols

 

         WAIT myArray[count1,count2] WINDOW AT 20,20

 

      ENDFOR

 

   ENDFOR

 

ENDIF

 

 

 

DEFINE CLASS myhandler AS Session

 

   PROCEDURE myresize

 

   _obrowser.left = _SCREEN.Width - _obrowser.width

 

   RETURN

 

ENDDEFINE

See Also

Tasks

Bind, Raise, Unbind, and Retrieve Events Sample

Reference

BINDEVENT( ) Function
UNBINDEVENTS( ) Function
RAISEEVENT( ) 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

© Microsoft Corporation. All rights reserved.

 

以上说明很多:

简单例子:  form1->grid1->columns[X].text1  现希望双击删除记录

form1.init ->

WITH this
   FOR i=1 TO .grid1.ColumnCount
     BINDEVENT(.grid1.Columns[i].text1,'DBlCLICK',this,"delrec")    
   NEXT   
ENDWITH

delrec->form.delrec->

IF MESSAGEBOX('ÊÇ·ñɾ³ý±¾Ìõ¼Ç¼?',1,'Ìáʾ')=1 THEN
   SELECT allxs
   DELETE
   thisform.Refresh  
ENDIF

 

0

评论Comments