Opening a child form from another child form and set MDI to parent form - how to do?

Saeid Yazdani picture Saeid Yazdani · Nov 1, 2011 · Viewed 52.8k times · Source

I have a MDI form. within this MDI form I can open some child forms using:

This is within MainForm

Form1 f1 = new Form1;
f1.MdiParent = this; //this refers to MainForm (parent)
f1.Show();

This works as expected!

But Now, while I am in the child form (Form1 -> f1) I want to open another form as a child for MainForm but when I use this keyword it will reffer to f1. How can I open the new form within f1 and set its MdiParent to MainForm ?

Answer

Gabe Thorns picture Gabe Thorns · Nov 1, 2011

Try assigning the parent form of your first child from:

Form2 f2 = new Form2;
f2.MdiParent = this.ParentForm; //this refers to f1's parent, the MainForm
f2.Show();

Hope this helps.