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

Return Value from Child form in Parent Form

Status
Not open for further replies.

mstrcmtr

Programmer
Nov 14, 2007
103
PK
Calling from Parent Form from one of the command button click event method

Code:
LOCAL o_Child_Form As Form

DO FORM Child_Form NOSHOW NAME o_Child_Form LINKED ;
WITH ;
Var1 , Var2 , Var3 , Var4 

o_Child_Form.Show(1) && 1-Modal

How can return the value from Child Form to Parent form of Var1 , Var3
 
This is easy. In the Unload event:

Code:
RETURN <whatever you want to return>

But, of course, "whatever you want to return" must be in scope. And keep in mind that, by the time the Unload fires, all your controls on the form will have been destroyed. So, if you wanted to return the value of a textbox, for example, you would store that value in a custom form property, and return that propery in the Unload.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
By the way, you can't do this after you run the form:

Code:
o_Child_Form.Show(1) && 1-Modal

Instead, you need to set the WindowType to 1 at design time. That will make the form modal. If it is not modal, it can't return a value.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If you thought parameters passed by the WITH clause are automatically by reference - that's only so for DO, the DO FORM command is a separate command.

You can pass in an object parameter, they are always passed by reference, it can have the parameters as properties. When the values come from a record the easiest way to create such an object is by SCATTER fld1, fld2, fld3 NAME loObjectvariable.

To keep this object in scope you need to store it into a property of the called form. That even works, when you defined the variable as LOCAL in the calling code of a parent form. If the called form then changes THISFORM.paramobject.param2 this will be available in the calling form, even without a RETURN from Unload.

Chriss
 
Some years ago we discussed something like that in the german VFP forum. The consensus at that time was, to us a parameter object bound to _screen. This object can hold individual properties an array or simply one prop with an XML stream or even JSON. It simply depends on the amount of values and the frequency. Perhaps on personal preferences, too :)

One other way I've used for years is to create a property in the calling form as well as in the called form.
The called form gets this property filled by the caller with an caller.property reference. Within calledform.Release I evaluate the reference and write all values as a commaseparated string into the callerforms property.
The callerform.property has an Assign method that reacts to the given value.
The important point in this is, not to write a direct object reference into the calledform.property. Just a string that gets evaluated.

Therefore, the _screen.paramObj is a better solution if you're not used to the above.

JM2C

-Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top