Link to home
Start Free TrialLog in
Avatar of TerryBurger
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;
            }
Avatar of cyberdevil67
cyberdevil67

Form2 form2 = null;
Form3 form3 = null;

private void button3_Click(object sender, System.EventArgs e)
          {
               form2.Close();
               form3 = new Form3();
               form3.Show();
          }
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();
}
Avatar of TerryBurger

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
Avatar of BurntSky
BurntSky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are doing this in you MDI container


  Form form1 = new myForm();

 then you can just do this

 form1.close();