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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP6: Creating controls on the fly/assigning events? 1

Status
Not open for further replies.

Webrookie

Programmer
May 18, 2000
112
US
I can create my controls all fine and dandy, but I want to point their events to a function I wrote. This is easily done in VB6, but I need it for VFP6. Here's a sample of what I have:
------------------------------------------------------------
with thisform
.addobject("txt"+alltrim(questions->disporder),"TEXTBOX")
contname="txt"+alltrim(questions->disporder)
.&contname..left=25
.&contname..top=lastbottom
.&contname..value=""
.&contname..visible=.t.
.&contname..tabindex=lasttab
.&contname..maxlength=255
.&contname..width=255
.&contname..Valid=ValidAns(.&contname)
endwith
------------------------------------------------------------
As you can see I'm trying to assign the Valid Event of the textbox to the ValidAns function.

When I run the project and leave the textbox, the code in the ValidAns function actually runs, but then I get an error saying the valid property is read only.

any ideas?

 
Valid is not a property it is a method. You cannot set a method. Why don't you create a textbox class with the proper validation code in it and instanciate that instead of a base textbox class?

 
Yeah, that was my next step if VFP couldn't perform like VB

poop.

thanks.
 
You need to get over the VB mentality. VFP is object oriented with inheritance. VB is not. IMHO no serious development should be done with base classes. You should always create your own subclasses and use those.

 
Webrookie, it is possible but another way you think.

In VFP there are no direct way to assign code to the method or event. However, following works very well for VFP-based objects:

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


with thisform
contname="txt"+alltrim(questions->disporder)
.addobject(contname,"TEXTBOX")
with eval('.'+contname)
.left=25
.top=lastbottom
.value=""
.tabindex=lasttab
.maxlength=255
.width=255

private lcToBeHooked
m.lcToBeHooked = eval('.'+contname)
set procedure to hook.PRG
thisform.AddObject('hook'+contname,'MyHook')
&& after this moment the oHookedObject.Valid code
&& is called from this object
.visible=.t.
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. 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.


PS: I rearranges and changed your code to show you the best and the most quick code.
Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
Wow Vlad!

You have blown my mind again. My brain is rethinking several recent reproblems and exploring alterante hooked solutions as I type this.

I haven't been here for a while but that is a really nifty trick. You should consider copying your responce and placing it in the FAQ for this board.

BTW what ever happend to your alias ThomasDill?
-Pete
 
Hi!

Right, this approach is quite powerful. It was invented when trying to find a way to hook into the events of the _screen object - to catch when it is resized. Usually people do this through timer, but recently I read this new approach at teh and tried to play with it. Quite amazing.

I wonder, why MS VFP team cannot make that just as a single command 'Object.Method:=Code' when we can do this using native VFP code? And add the functionality of VFPCOM.DLL to this? Well, the answer is just here and there...

I will not put it into FAQ, I cannot tell that this question is frequently asked, but if others vote for this, I will do it.

PS: Tomas Dill was my alias in the computer games. Now this is the last place where I used it.
Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top