Condividi tramite


EventHandler( ) Function

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

EventHandler(oCOMObject, oVFPObject [, lUnbind])

Return Values

Logical. (.T.) if successful.

Parameters

  • oCOMObject
    Specifies the object reference to the object whose events are to be bound.
  • 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 stops event handling when either object goes out of scope. By passing this parameter, you can stop event handling while both objects remain in scope.

Remarks

oCOMObject must be a valid COM object.

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

DEFINE CLASS Command