Open form B from form A and catch return value from form B

Hi,

I’d like to press a button on form A to open form B.
From form B, I need to acquire a return value and use it in form A.
How can I do this?
Thanks

Hi Tim,

The example helped me but didn’t solve the problem.
I’d like to capture the value returned by form B using the “Return myvalue” command, not the value selected in form B.
I hope I’ve explained myself clearly.

I’m sorry, but it’s not very clear what the difference is. You desire to get a value from Form B and use it in the code on Form A, correct?

Correct.
But if I put in Press Button into form newpwd1 “Return myvalue”, what code should I write in form A?

In Form A I have:
var whatnewpwd as string
whatnewpwd= “”

Var prompt As newpwd1
prompt = New newpwd1
prompt.ShowModal

whatnewpwd= prompt.returnvalue

Where am I going wrong?

From that code snippet, if the whatnewpwd is empty, I would guess the problem is the returnvalue on Form B.

Make sure you move the text out of UI controls on Form B into properties of Form B before Form B is closed. Once Form B is closed, you can’t access any of the controls (so for example accessing FormB.txtPassword.Text will raise a NilObjectException). That’s what the Example Project from above shows you in the AddAndClose function.

I’m not sure what else to say. The Example Project shows you how to do what you are asking, there’s just a hiccup in your implementation.

Little example to do this.

Voilà !

Eric,
I figured out the trick you showed me, and it might even work.
I don’t close Window 2, but I hide it so that it can get the value from Window 1.
But it gives me an error, which you can see at the bottom of the figure.
Keep in mind that form 2 is called newpwd1, and the textfield in form 2 whose value I want to return is called newmpwd1.
I don’t understand why it’s giving me an error.

What is “newmpwd1” ?

If newmpwd1 is a textfield you have to declare it “public” like above “scope : public”

ShowModal is going to block your code and show the Window until it is closed, after that your controls would of been removed and be nil, so the values are no longer accessable. What you need to do is save a local public property with the return values (you can have as many as you want). These can then be accessed without problem on the next line after showModal.

When you click a save button, or on the closing event on the window, you can grab the value from the text field and save it in your local property. Then you can access it from outside without issue.

newpwd1.close() is 100% not going to work, as showmodal means the window must of closed to get to the next line.

Here’s an example of how you can use a modal window as an input prompt and get feedback whether the ok or cancel button was pressed on that input window.

Modal Return Value.xojo_binary_project.zip (7.1 KB)

Ok Eric,
Setting the variable to Public seems to work.

Thanks.

Hi Graham,

For now, I’m using Eric’s method, which seems to work.
I’ll keep your suggestions in mind, though.
Thank you very much.

Hi Jared,

I’ll take a look at the example you sent me.
Thanks