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.