Possible Duplicate:
How do you pass an object from form1 to form2 and back to form1?
I'm used to passing variables between windows forms by simply passing them as a parameter.
Now I have a form that is already open (let's call it FormMain
), and another form that should act like a dialog (FormTask
). The user cannot interact with the main form until he has filled in the information on FormTask
. FormTask
simply contains a single textbox, and the value of this textbox should be returned to FormMain
, and kept track of as a variable. FormTask
requires a parameter exerciseType
. When FormTask
opens it checks the value of this parameter and sets the default value of the textbox accordingly. This already works, I'm just kind of clueless on how to return my string value to the already open MainForm
.
These dialogs only seem to be able to return DialogResults
, which isn't what I'm after. I'm not too experienced either, and I'd rather avoid fumbling around to make my own custom dialog.
FormMain:
FormTask formTask = new FormTask(exerciseType);
formOpgaveInvoker.ShowDialog();
FormTask:
private void button1_Click(object sender, EventArgs e)
{
string opgave = textBoxOpgave.Text;
// return string value to MainForm here
}
To send data from Parent to Child form you need to create a parameterized constructor. To send data from Child to Parent form you need to create a public method in Child class and use the method in Parent class to get the return value.
Create public property in FormTask
public string Opgave { get {return textBoxOpgave.Text;}}
And check it after ShowDialog();
FormTask formTask = new FormTask(exerciseType);
formOpgaveInvoer.ShowDialog();
formOpgaveInvoer.Opgave; // here it is
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With