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

5

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 ?

Marsland answered 1/11, 2011 at 13:5 Comment(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.Religionism
G
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.

Geranium answered 1/11, 2011 at 13:19 Comment(0)
A
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();
Adeleadelheid answered 1/11, 2011 at 13:18 Comment(0)
C
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();
Clique answered 1/11, 2012 at 6:2 Comment(0)
A
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()
Asphyxia answered 21/8, 2020 at 2:59 Comment(0)
P
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.

Psychrometer answered 19/9, 2020 at 4:10 Comment(1)
Thanks for answering a 10-year-old question :)Marsland

© 2022 - 2024 — McMap. All rights reserved.