13

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 ?

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • 1
    You probably could use this.MdiParent. It is usually best to leave creating child windows to the parent. Use an event to tell it to take action. – Hans Passant Nov 01 '11 at 13:20

5 Answers5

42

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.

Gabe Thorns
  • 1,426
  • 16
  • 20
12

Let us suppose that the second form is f2.Then, the code in form f1 to create a new form f2 in MDI parent form will be:

Form2 f2 = new Form2;
f2.MdiParent = this.MdiParent;
f2.Show();
Kevin J F
  • 5
  • 3
Mazen313
  • 520
  • 4
  • 14
5

Well, not to argue with the "solution" that was listed... but if I'm understanding the request correctly and trying the above solution didnt work i would do the following....

Form2 f2 = new Form2();
        f2.MdiParent = MDIParent1.ActiveForm;
        f2.Show();
user1762132
  • 215
  • 1
  • 3
  • 9
0

Let us suppose that the second form is frm2.Then, the code in form frm1 to create a new form frm2 in MDI parent form will be: create new object then again retrived data mdiparent forms solved freeze dispose form

Dim dru as New frm2 '// another form call
dru = New frm2
dru.mdiparent = frm1 '// main forms
dru.show()
Hemakumar
  • 1
  • 1
0

I had the same problem and tried all different solutions. Finally the one that worked for me was:

Dim ChildForm As New AddingText("")
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = MDIParent1
ChildForm.Dock = DockStyle.Fill
MDIParent1.m_ChildFormNumber += 1
ChildForm.Text = "Client Existent" & MDIParent1.m_ChildFormNumber
ChildForm.Show()

the hiccup is that could not be used in conjunction with ShowDialog(), but i can live with it.