Share via


EVENTHANDLER( ) Function

Binds a COM server event to implemented interface methods on a Visual FoxPro object.

For information about binding events from native Visual FoxPro objects, see Event Binding for Visual FoxPro Objects.

EVENTHANDLER(oCOMObject, oVFPObject [, lUnbind])

Parameters

  • oCOMObject
    Specifies the object reference to the object whose events are to be bound. oCOMObject must be a valid COM object.

  • oVFPObject
    Specifies the object reference to the Visual FoxPro object that contains user-defined method code, which maps to corresponding COM object events. The class must implement the events interface for the particular COM object being passed in the first parameter. For example, you must implement the RecordsetEvents interface to bind to an ADO Recordset object.

  • lUnbind
    Visual FoxPro automatically releases the COM object when it goes out of scope. By passing this parameter with a value of .T., you can release the event binding while both objects remain in scope.

Return Value

Logical data type. EVENTHANDLER( ) returns True (.T.) if successful and False (.F.) if unsuccessful.

Remarks

You can bind a single COM object to several Visual FoxPro objects, or you can bind several COM objects to the same Visual FoxPro object.

Event handling is automatically unbound when either the Visual FoxPro or the COM object is released. You can explicitly call the EVENTHANDLER( ) function and pass the lUnbind parameter to unbind objects without releasing object references.

Example

LOCAL oEvents
LOCAL oRS AS adodb.recordset
LOCAL oConn AS adodb.Connection

oEvents = NEWOBJECT("myclass")
oConn = NEWOBJECT("adodb.connection")

oConn.Provider="MSDASQL"
* Make sure to set the SourceDB property 
* below to your TESTDATA location.
oConn.ConnectionString="DSN=Visual FoxPro Database;" + ;
"SourceType=DBC; SourceDB=D:\VFP\DATA\TESTDATA.DBC"
oConn.Open
oRS = oConn.Execute("select * from customer")
? EVENTHANDLER(oRS, oEvents)
?
? PADR(oRS.Fields(0).Value,20)
? EVENTHANDLER (oRS, oEvents, .T.)
oRS.MoveNext
? PADR(oRS.Fields(0).Value,20)
oRS.MoveNext
CLEAR all
RETURN

DEFINE CLASS myclass AS session
IMPLEMENTS RecordsetEvents IN "adodb.recordset"

PROCEDURE Recordsetevents_WillChangeField(cFields AS Number @, Fields AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_FieldChangeComplete(cFields AS Number @, Fields AS VARIANT @, pError AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_WillChangeRecord(adReason AS VARIANT @, cRecords AS Number @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_RecordChangeComplete(adReason AS VARIANT @, cRecords AS Number @, pError AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_WillChangeRecordset(adReason AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())
   ?adreason,adstatus,precordset.recordcount

PROCEDURE Recordsetevents_RecordsetChangeComplete(adReason AS VARIANT @, pError AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_WillMove(adReason AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_MoveComplete(adReason AS VARIANT @, pError AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_EndOfRecordset(fMoreData AS LOGICAL @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_FetchProgress(Progress AS Number @, MaxProgress AS Number @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

PROCEDURE Recordsetevents_FetchComplete(pError AS VARIANT @, adStatus AS VARIANT @, pRecordset AS VARIANT @) AS VARIANT
   ? " "+program() + ' ' + TRANSFORM(DATETIME())

ENDDEFINE

See Also

Reference

DEFINE CLASS Command

Other Resources

Functions

Language Reference (Visual FoxPro)