Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Return value from form
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Gestionnaire d'écran & Écrans
Divers
Thread ID:
00809666
Message ID:
00809670
Vues:
24
Hi Leroy.

I have a "Modeless" form that needs to call another form and receive a
value form that form. Is there a way to do this without using public variables?


This is what we had to say on the subject in Chapter 13 of 1001 Things You Wanted to Know About Visual FoxPro

How do I return a value from a modal form?

Elsewhere, we have already discussed some techniques for passing parameters between objects of varying types. What we have not covered specifically anywhere else are the various techniques for getting values back from a modal form. The concept of "returning a value" is only meaningful when the form returning the value is modal because the implicit requirement is that some process must be interrupted, or suspended, until the required value is returned. Only by using a modal form can you ensure that:
• The process cannot continue until the value is available
• The value is returned to the correct place in the calling code.

There are essentially three ways of getting a value back from a modal form, but one of them works only for forms which are run as SCX files, one works with forms that are instantiated from VCX files and one works irrespective of how the form is created. Remember that, although we are talking about returning 'a value', this 'value' could be a parameter object containing multiple items (see Chapter 2 "Functions and Procedures" for more information on creating and using parameter objects).

Returning a value from a form
The actual mechanism for returning a value from a form is quite straightforward. You simply place a RETURN command as the last line of code in the form's UNLOAD method. This is the last form method to be executed before a form is released and so it is a perfectly logical place for the return statement. However, there is one little catch. If the value that you wish to return is coming from a control on the form, by the time the form's Unload method runs all controls have been released, so the values that they held will no longer be available. To get around this problem you must ensure that any control values you wish to return are saved to form properties no later than in the Form's Destroy method. (See the section earlier in this chapter for details of the event sequence when a form is instantiated or destroyed.)

Hiding a modal form
One way of getting access to values that are contained within a modal form is simply to hide the form instead of releasing it. When a modal form is hidden, whichever form was active immediately prior to the modal form being instantiated becomes the active form once again. In other words, the form that called the modal form is re-activated. Providing that you have, within the calling form, a valid reference to the modal form you can access any exposed properties of the form, or of its contained controls. This approach will work irrespective of the way in which the form is instantiated.
The following code snippet shows how this might be done for a form instantiated directly from a class:
*** Instantiate a modal form
oFm = NEWOBJECT( 'modalform', 'formclasses' )
*** Show the form and ensure that it is modal
oFm.Show(1)
*** When form is 'released' it is actually hidden!
*** Access the Modal form's properties directly
ThisForm.SomeProperty = oFm.ModalFormProperty
*** Release the modal form when done
oFm.Release()
While for a form that is created from an SCX file, the following code is equivalent:
*** Instantiate the modal form
DO FORM modalform NAME oFm LINKED
*** When the modal form is "released" it is actually hidden!
*** The NAME 'oFm' can now be used to access it directly:
ThisForm.SomeProperty = oFm.ModalFormProperty
*** Release the modal form when done by releasing the "linked name"
RELEASE oFm
Using do form name to variable
For modal forms that have been created as SCX files and which are run using the DO FORM command, there is a specific syntax you use to save a value which is returned from the form, as follows:
DO FORM modalform TO luRetVal
When the modal form is released, whatever was returned from the form's Unload method will be saved to the variable 'luRetVal'. Note that this variable does not need to have been previously declared and will be created as needed. However, if the called form does not contain a RETURN statement in its Unload method, the variable will NOT be created. So, in our opinion, it is much safer to always explicitly declare the return variable, and initialize it, rather than just relying on there being a return statement.

Returning a value from a form instantiated directly from a class
The problem in this situation is that there is no way to return both the object reference AND a return value from either the CreateObject or NewObject functions. Since both must return a reference to the new object we have to find another way of getting a value back. The solution is to pass a parameter object to the form that can then be returned by the modal form when it is released.

The form class must be set up to receive, and store to a property, the parameter object that is passed to it. (Normally we would also have the class Init method call its Show method directly to make the form visible immediately on instantiation.). This object must be populated with the relevant properties while the form is active and returned to the calling method (or procedure) from the modal form's Unload method. The code to instantiate the modal form would look like this:
*** Create the Parameter object
oParamObj  = CREATEOBJECT( 'parameterobject' )
*** Instantiate the modal form
oModalForm = CREATEOBJECT( 'modalformclass', oParamObj )
*** Check the returned object properties
IF oParamObj.FormWasOK
  *** Do whatever 
ELSE
  *** Do something else
ENDIF
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform