Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign code to event in run-time?

Coding and Syntax

How to assign code to event in run-time?

by  TomasDill  Posted    (Edited  )
In VFP there are no direct way to assign code to the method or event. However, some workaround is possible with use of additional, 'hook' class:

* hook.PRG
DEFINE CLASS MyHook
oHookedObject = eval(m.lcToBeHooked)
procedure oHookedObject.Valid
&& your code here
endproc
ENDDEFINE


Than add this object to the form or the container that require to use it:

with thisform.MyObjectRequiredToeHooked
private lcToBeHooked
m.lcToBeHooked = thisform.MyObjectRequiredToeHooked
set procedure to hook.PRG
thisform.AddObject('MyObjectHook','MyHook')
&& after this moment the oHookedObject.Valid code
&& is called for Valid event of the MyObjectRequiredToeHooked
endwith

You can build a hook.PRG file on the fly with the code you require in a string, save to file, set procedure to it, create an object. But do not release PRG from memory until you destroy the form/container having hook object. So when you need this quite universal, you have a good reason to track all hooks or even build hooks manager class for these purposes that will store all hooks and release the when they're free. The approach is not tested in the live environment, though it works very well for a single use in such cases as assigning code to the _Screen form events.

For ActiveX objects you require to use the VFPCOM.DLL to bind VFP code to the ActiveX object event. Thedescribed approach works with VFP native objects only.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top