INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Coding and Syntax

How to assign code to event in run-time? by TomasDill
Posted: 23 Jul 01

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.

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close