Reputation: 2989
I open a childform(form2) from another childform(form1) and set MDI to parent form.
Here is how I open form1 as childform of MainForm from MainForm
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form1 f1 = new form1();
f1.MdiParent = this;
f1.Show();
}
}
Here is how form2 as another childform of MainForm from form1 which is also a childform of MainForm
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2 f2 = new form2();
f2.MdiParent = this.ParentForm;
f2.Show();
}
}
How I will pass a value to form2 from form1? I tried what I am doing to pass a value to modal form but did not works
form1
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2 = new form2();
f2.MdiParent = this.ParentForm;
f2.name = textBox1.Text;
f2.Show();
}
}
form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string NAME
public string name
{
get { return NAME; }
set { NAME = value; textBox1.Text = NAME; }
}
}
Upvotes: 0
Views: 503
Reputation: 1055
You can set MdiParent
, and also call MdiParent
public partial class MainForm : Form // IsMdiContainer = true in Properties
{
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.MdiParent = this;
form1.Show();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this.MdiParent;
form2.tb = textBox1.Text;
form2.Show();
}
}
public partial class Form2 : Form
{
private string v;
public Form2()
{
InitializeComponent();
}
public string tb
{
get { return textBox1.Text; }
set { v = value; textBox1.Text = v; }
}
}
Upvotes: 0
Reputation:
I think the main problem is the event handler you're using for the button1 click Form1
because you posted two event handlers for button1 in Form1
but i tried this and it worked. Put it in the event handler for button1 click in Form1
private void button1_Click(object sender, EventArgs e)
{
//Create a new Instance of Form2
Form2 f2 = new Form2();
//Sets the MDI Property
f2.MdiParent = this.ParentForm;
//Shows the Form
f2.Show();
//Open the already created instance
Form2 f = (Form2)Application.OpenForms["Form2"];
//Update the Property
f.name = textBox1.Text;
}
Upvotes: 1
Reputation: 7963
When I pass values from one form to an other I usually create a parameter object
public class FormParams
{
public string Name {get; set;}
}
In the form that will receive the paramater
public class Form2
{
public FormParams Parameters {get; set;}
...
}
In the form that will make the call you would do something like
FormParams frmParams = new FormParams();
frmParams.Name = "KarlX"
Form2 form = new Form2();
form.Parameters = frmParams;
form.MdiParent = this;
form.Show();
When you want to use the value passed to the form just use
txtStackOverFlowName.Text = Parameters.Name;
Upvotes: 0
Reputation: 1055
This worked for me, you didn't have constructors with InitializeComponent
calls and your second code posting didn't have the assign to call.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.tb = textBox1.Text;
form2.Show();
}
}
public partial class Form2 : Form
{
private string v;
public Form2()
{
InitializeComponent();
}
public string tb
{
get { return textBox1.Text; }
set { v = value; textBox1.Text = v; }
}
}
Upvotes: 0