Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing value from dialog form to main form [duplicate]

Tags:

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
}
like image 715
Jort Avatar asked May 17 '11 09:05

Jort


People also ask

How pass data from parent form to child form in C#?

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.


1 Answers

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
like image 81
Stecya Avatar answered Sep 19 '22 15:09

Stecya