none
Return value from one form to another

    Question

  • I know that you can do a form with a value. But, I have a form that calls another little form that allows the user to select a picture from a list. I want to release the second form and return to the first form with a value = to the picture location for the picture selected in this second form.

     

    Any Ideas?

    Sunday, January 04, 2009 3:25 AM

Answers

  • You may try to use a PUBLIC variable, a field in a table, put value in a text file or jus put the value in a object in first form when close the second form .....

    Sunday, January 04, 2009 4:46 AM
  • Hi,

     

    DO FORM <FORM_NAME> WITH <PARAMETERS_LIST> TO <VAR_TO_STORE_RETURN_VALUE>

     

    EG.

    DO FORM SEARCH WITH ALIAS() TO RTNVAL

     

    Here RTNVAL will store the returned value.

     

     

    Sunday, January 04, 2009 10:31 AM
  • Providing that the second form is modal (WindowType = 1) then you can use the

    following syntax of the DO FORM command:

     

    Code Snippet

    LOCAL lcRetVal

    DO FORM your_formname TO lcRetVal

     

     

    However, in order to do this, you must do two things in the form that is getting the value:

    • First, the value must be returned from the UNLOAD() method of the form using a RETURN statement.
    • Second, the value must be stored to a PROPERTY on that form because when the Unload fires, all controls on the form have already been destroyed and so any values associated with them have also gone.

    So what you have to do is to create a property on your form that gets the path (in the form designer go to the main menu and choose "FORM => New Property and name it 'RetVal' - or some such suitable name). Then in your form store the value of the path to that property (the valid of your textbox should be the place to do that). Finally in the form's UNLOAD() add the following line of code:

    Code Snippet
    RETURN ThisForm.RetVal

     

     

    An alternative is to use the NAME option of the DO FORM command to create a local reference to the form:

    Code Snippet

     

    LOCAL loForm

    DO FORM your_formname NAME loForm

     

     

    Now, in your target form, instead of ThisForm.Release(), use ThisForm.Hide() to close the form. This removes the form from the screen, but it still exists and so your local reference is still valid. You can use that reference to read the value directly from whatever control has it on the child form and then use the same local reference to release the form:

     

    Code Snippet

    LOCAL loForm

    DO FORM your_formname NAME loForm LINKED

    ThisForm.PictureLoc = loForm.TxtPath.Value

    loForm.Release()

     

     

    Again your child form must be modal in order for this to work!

    Sunday, January 04, 2009 11:29 AM

All replies

  • You may try to use a PUBLIC variable, a field in a table, put value in a text file or jus put the value in a object in first form when close the second form .....

    Sunday, January 04, 2009 4:46 AM
  • Hi,

     

    DO FORM <FORM_NAME> WITH <PARAMETERS_LIST> TO <VAR_TO_STORE_RETURN_VALUE>

     

    EG.

    DO FORM SEARCH WITH ALIAS() TO RTNVAL

     

    Here RTNVAL will store the returned value.

     

     

    Sunday, January 04, 2009 10:31 AM
  • Providing that the second form is modal (WindowType = 1) then you can use the

    following syntax of the DO FORM command:

     

    Code Snippet

    LOCAL lcRetVal

    DO FORM your_formname TO lcRetVal

     

     

    However, in order to do this, you must do two things in the form that is getting the value:

    • First, the value must be returned from the UNLOAD() method of the form using a RETURN statement.
    • Second, the value must be stored to a PROPERTY on that form because when the Unload fires, all controls on the form have already been destroyed and so any values associated with them have also gone.

    So what you have to do is to create a property on your form that gets the path (in the form designer go to the main menu and choose "FORM => New Property and name it 'RetVal' - or some such suitable name). Then in your form store the value of the path to that property (the valid of your textbox should be the place to do that). Finally in the form's UNLOAD() add the following line of code:

    Code Snippet
    RETURN ThisForm.RetVal

     

     

    An alternative is to use the NAME option of the DO FORM command to create a local reference to the form:

    Code Snippet

     

    LOCAL loForm

    DO FORM your_formname NAME loForm

     

     

    Now, in your target form, instead of ThisForm.Release(), use ThisForm.Hide() to close the form. This removes the form from the screen, but it still exists and so your local reference is still valid. You can use that reference to read the value directly from whatever control has it on the child form and then use the same local reference to release the form:

     

    Code Snippet

    LOCAL loForm

    DO FORM your_formname NAME loForm LINKED

    ThisForm.PictureLoc = loForm.TxtPath.Value

    loForm.Release()

     

     

    Again your child form must be modal in order for this to work!

    Sunday, January 04, 2009 11:29 AM
  • I think this must work.

    When in the first form, call the second form by using ".ShowDialog()" rather than using ".Show()". I will try to illustrate it here with an example.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSelectMachine.Click

            If frmPartForMachine.ShowDialog() = Windows.Forms.DialogResult.OK Then
           
        lblMachineID.Text = frmPartForMachine.machineid

            End If

        End Sub

    * Here
    frmPartForMachine is my second form where i want to retrieve a value in my form.
    *
    frmPartForMachine.machineid is a property of the second form that holds the required value.

    Then - in my second form - on an event - Say- Close or select or save or whatever. Add the following , but remember, you must assign the property the required value right above this statement.

     Me.DialogResult = Windows.Forms.DialogResult.OK


    Saturday, January 10, 2009 11:02 AM
  • Mr. Naser,

    That code wouldn't work natively in VFP. You need interop services. I think creating an interop form is not trivial for majority. But why would you want to create a COM interop form when you can do that easily in native VFP?

     

    The native coding of what you proposed is already showed by Andy. Read his message carefully. VFP also have more ways to accomplish this task.

    Saturday, January 10, 2009 1:37 PM
  • Mr. CetinBasoz,

    I agree with you.

    I went through his post again after your post thinking that perhaps i may have missed out something important. He figured it out correctly. I just wanted to express the same thing in some other way by another example.
    Sunday, January 11, 2009 10:27 AM