Passing info back from a form to a form which is open

J

jed

If on form2 i calculate a value how do i pass it back to form form1.I
want to close form2 and at the same time pass the value back to
form1.Thanks
 
T

Tomas Vera

Many options on this one.

1. Pass a reference to a variable in Form1 when creating form2
2. Declare/expose a variable in Form2 that "points" to a variable on
Form1
3. Declare a delegate function in Form2 that points to a function on
Form1 that sets the variable.

Example of 1:
Form1:
private void LaunchForm2()
{
int varToReturn = 0;
Form2 form2 = new Form2(ref varToReturn);
form2.ShowDialog();

Console.WriteLine("Form2 returned: " + varToReturn.ToString());
}

Form2:
// CONSTRUCTOR
private int form1Var;
public Form2(ref int varFrom1)
{
form1Var = varFrom1;
}
private void OnClose()
{
form1Var = DoSomething();
}


Hope this helps.
-tomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top