22

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
}
Community
  • 1
  • 1
Jort
  • 1,401
  • 8
  • 23
  • 39
  • Since everyone in front of a keyboard has already posted an answer, I won't try to do so myself. But do keep in mind that you should still check the `DialogResult` value returned by the call to `ShowDialog`. You want to allow for the case where the user *canceled* (i.e., closed the form without entering anything at all), and you don't want to treat whatever they entered in the textbox as actual input. – Cody Gray - on strike May 17 '11 at 10:00

4 Answers4

34

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
Stecya
  • 22,896
  • 10
  • 72
  • 102
1

The simplest way to do this is to add a public property to your form class to return the string.

public string opgave
{
    get;
    private set;
}

Assign to this property as your dialog closes and then read the property from the code that called ShowDialog():

private void button1_Click(object sender, EventArgs e)
{
    opgave = textBoxOpgave.Text;
}

...

FormTask formTask = new FormTask(exerciseType);
formOpgaveInvoker.ShowDialog();
DoSomething(formTask.opgave);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 3
    Alternatively, you could simply have the property set and return `textBoxOpgave.Text`. Saves a lot of irrelevant lines of code. When you show a form with `ShowDialog`, the controls are guaranteed not to be disposed, precisely so you can read their values. That's why you need to wrap the creation of such forms in a `using` statement. – Cody Gray - on strike May 17 '11 at 10:02
1

Forms are just normal classes. This means, you can create properties in them.
So: Create a property and assign the value to it.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

Add a property to the FormTask for example String1 like

public string String1 {get; set;}

Set String1 value in button1_Click for example,

You can access that property in MainForm like

FormTask formTask = new FormTask(exerciseType);   
formOpgaveInvoer.ShowDialog(); 
string str = formTask.String1;
FIre Panda
  • 6,537
  • 2
  • 25
  • 38