Search code examples
c#formsmdi

MDI Child Forms Opening each other with the same parent form


So I have 3 Forms, lets call them Form1, Form2, and Form3. I have sent the IsMDIParent Property to true for Form1.

When I launch the app, It loads Form2 as an MDI Child using

Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();

And that works fine. What I then want to do is click a button withing the 2nd form that will close Form2 and open up Form3 as a child form of Form1.

I tried

SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = Form1;
SecondFormMDI.Show();

on the button click event in Form2, but it would not work.

Do I have to always launch a Child form from the parent form? and if so, how would i go about doing that when it is on the button click event on a child form?


Solution

  • Just use this.MdiParent, instead of Form1, like

        SecondForm SecondFormMDI = new SecondForm();
        SecondFormMDI.MdiParent = this.MdiParent;
        SecondFormMDI.Show();