tags:

views:

5808

answers:

10

How do I pass a value from a child back to the parent form? I have a string that I would like to pass back to the parent.

I launched the child using:

    FormOptions formOptions = new FormOptions();
    formOptions.ShowDialog();

Many thanks

+13  A: 

Create a property (or method) on FormOptions, say GetMyResult:

using (FormOptions formOptions = new FormOptions())
{
    formOptions.ShowDialog();

    string result = formOptions.GetMyResult;

    // do what ever with result...
}
Mitch Wheat
A: 

Many ways to skin the cat here and @Mitch's suggestion is a good way. If you want the client form to have more 'control', you may want to pass the instance of the parent to the child when created and then you can call any public parent method on the child.

kenny
I prefer child forms nnot to know about who's calling them where possible. Less coupling...
Mitch Wheat
+11  A: 

You can also create a public property.

// Using and namespace...

public partial class FormOptions : Form
{
    private string _MyString;    //  Use this
    public string MyString {     //  in 
      get { return _MyString; }  //  .NET
    }                            //  2.0

    public string MyString { get; } // In .NET 3.0 or newer

    // The rest of the form code
}

Then you can get it with:

FormOptions formOptions = new FormOptions();
formOptions.ShowDialog();

string myString = formOptions.MyString;
stiduck
erm, Isn't that what I said already??
Mitch Wheat
It does the same, just with a property insted of a method.
stiduck
+6  A: 

If you're just using formOptions to pick a single value and then close, Mitch's suggestion is a good way to go. My example here would be used if you needed the child to communicate back to the parent while remaining open.

In your parent form, add a public method that the child form will call, such as

public void NotifyMe(string s)
{
    // Do whatever you need to do with the string
}

Next, when you need to launch the child window from the parent, use this code:

using (FormOptions formOptions = new FormOptions())
{
    // passing this in ShowDialog will set the .Owner 
    // property of the child form
    formOptions.ShowDialog(this);
}

In the child form, use this code to pass a value back to the parent:

ParentForm parent = (ParentForm)this.Owner;
parent.NotifyMe("whatever");

The code in this example would be better used for something like a toolbox window which is intended to float above the main form. In this case, you would open the child form (with .TopMost = true) using .Show() instead of .ShowDialog().

A design like this means that the child form is tightly coupled to the parent form (since the child has to cast its owner as a ParentForm in order to call its NotifyMe method). However, this is not automatically a bad thing.

MusiGenesis
+4  A: 

You can also create an overload of ShowDialog in your child class that gets an out parameter that returns you the result.

public partial class FormOptions : Form
{
  public DialogResult ShowDialog(out string result)
  {
    DialogResult dialogResult = base.ShowDialog();

    result = m_Result;
    return dialogResult;
  }
}
yapiskan
I like this. Dialog windows have always felt a little weird to me, since you're showing a window and then getting its info after it's gone. This approach shows the window and retrieves the info all at once.
MusiGenesis
+6  A: 

I had this problem a while ago. I ended up using an event.

public delegate void PassBackSubFolderList(List<string> value);
public partial class frmAddChildFolders : Form
{
    public event PassBackSubFolderList SubFolderListPassBack;
    private List<string> _folderList = new List<string>();
    public frmAddChildFolders()
    {
        InitializeComponent();

    }
    private void btnApply_Click(object sender, EventArgs e)
    {
        if (SubFolderListPassBack != null)
        {
            SubFolderListPassBack(_folderList);
        }
        this.DialogResult = DialogResult.OK;
    }  
 }

It gave me better control over what could be returned.

Erin
A: 

I think the easiest way is to use the Tag property in your FormOptions class set the Tag = value you need to pass and after the ShowDialog method read it as

myvalue x=(myvalue)formoptions.Tag;
Ahmed Said
I prefer not to use the Tag on classes that I have control of (i.e. where I can add a property). I think it is cleaner to use a property with a proper name and type.
Xavier Poinas
+1  A: 

Use public property of child form

frmOptions {
     public string Result; }

frmMain {
     frmOptions.ShowDialog(); string r = frmOptions.Result; }

Use events

frmMain {
     frmOptions.OnResult += new ResultEventHandler(frmMain.frmOptions_Resukt);
     frmOptions.ShowDialog(); }

Use public property of main form

frmOptions {
     public frmMain MainForm; MainForm.Result = "result"; }

frmMain {
     public string Result;
     frmOptions.MainForm = this;
     frmOptions.ShowDialog();
     string r = this.Result; }

Use object Control.Tag; This is common for all controls public property which can contains a System.Object. You can hold there string or MyClass or MainForm - anything!

frmOptions {
     this.Tag = "result": }
frmMain {
     frmOptions.ShowDialog();
     string r = frmOptions.Tag as string; }
abatishchev
A: 

I'm kinda confused. I thought when execution resumes in the parent form after the child form is closed, the variables in the child form should be gone. How come we can still access its properties?

FormOptions formOptions = new FormOptions(); formOptions.ShowDialog();

string myString = formOptions.MyString;

Ethan
The form is closed. It is not destroyed.
Xavier Poinas
A: 

Thanks Xavier,

So when is the form actually destroyed? I tried calling Form.Dispose() and yet I'm still able to access its properties.

Ethan
There are two things: the .NET Form object and the underlying OS object (from the Windows API). Calling Dispose() will only destroy the OS object. The Form object, which is just a plain CLR object, will only be destroyed when it is garbage collected. That's when you are no longer holding any reference to that object, ie. when your variable goes out of scope or you assign null (or another reference) to it.
Xavier Poinas