TerryBurger
asked on
Closing a child form from an mdi container
On my container there are buttons that when clicked a form pops up. What I want to happen is when the next button is clicked the form that is in focus is closed and the next form appears in its place. Here is what i have so far:
private void button2_Click(object sender, System.EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
button6.Enabled = true;
button5.Enabled = true;
button4.Enabled = true;
button3.Enabled = true;
button2.Enabled = false;
}
private void button2_Click(object sender, System.EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
button6.Enabled = true;
button5.Enabled = true;
button4.Enabled = true;
button3.Enabled = true;
button2.Enabled = false;
}
This should close the first open Mdi Child, if one exists. If more than one window could be open at a time and you want to close them all, you could just create a for loop.
private void button2_Click(object sender, System.EventArgs e)
{
if(this.MdiChildren.Length > 0) this.MdiChildren[0].Close( );
Form3 form3 = new Form3();
form3.Show();
}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.MdiChildren.Length
Form3 form3 = new Form3();
form3.Show();
}
ASKER
the this.MdiChildren[0].Close( ); didn't work maybe I'm not defining my other pages as Mdichildren correctly. In order for that statement to work how should I go about doing that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are doing this in you MDI container
Form form1 = new myForm();
then you can just do this
form1.close();
Form form1 = new myForm();
then you can just do this
form1.close();
Form3 form3 = null;
private void button3_Click(object sender, System.EventArgs e)
{
form2.Close();
form3 = new Form3();
form3.Show();
}