How I will pass a string from MDI parent to child's modal Dialog?
MDI parent code to open child:
Form1 f1 = new Form1();
f1.MdiParent = this;
f1.Show();
Form1 code to open modal dialog
Form2 f2= new Form2();
f2.ShowDialog();
How I will pass a string from MDI parent to child's modal Dialog?
MDI parent code to open child:
Form1 f1 = new Form1();
f1.MdiParent = this;
f1.Show();
Form1 code to open modal dialog
Form2 f2= new Form2();
f2.ShowDialog();
If you always use a form in as a modal form, you can use a pattern similar to this.
class FormResult
{
public DialogResult dr {get; private set;}
public string LastName {get; private set;}
public string FirstName {get; private set;}
}
class MyForm : whatever
{
static public FormResult Exec(string parm1, string parm2)
{
var result = new FormResult();
var me = new MyForm();
me.parm1 = parm1;
me.parm2 = parm2;
result.dr = me.ShowDialog();
if (result.dr == DialogResult.OK)
{
result.LastName = me.LastName;
result.FirstName = me.FirstName;
}
me.Close(); // should use try/finally or using clause
return result;
}
}
... rest of MyForm
This pattern isolates the ways in which you use the "private" data of the form, and can easily be extended if you decide to add mors return values. If you have more that a couple of input parameters, you can bundle them into a class and pass an instance of that class to the Exec method
Just pass it through using properties at each level:
//Form1 needs a property you can access
public class Form1
{
private String _myString = null;
public String MyString { get { return _myString; } }
//change the constructor to take a String input
public Form1(String InputString)
{
_myString = InputString;
}
//...and the rest of the class as you have it now
}
public class Form2
{
private String _myString = null;
public String MyString { get { return _myString; } }
//same constructor needs...
public Form2(String InputString)
{
_myString = InputString;
}
}
Ultimately, your calls become:
String strToPassAlong = "This is the string";
Form1 f1 = new Form1(strToPassAlong);
f1.MdiParent = this;
f1.Show();
Form2 f2= new Form2(f1.MyString); //or this.MyString, if Form2 is constructed by Form1's code
f2.ShowDialog();
Now, each form along the way has a copy of the string you passed along.