MDI Parent Form Problem setting Parent

Afnan Bashir picture Afnan Bashir · Dec 11, 2010 · Viewed 19.3k times · Source

I am using a MDI parent form that has a childs and they show up very well when they are called up by this parent and i use to intensiate child form as

ChildForm child = new ChildForm();
child.IsMdiContainer= this;
child.Show();

works well as soon as they are called from parent control but if i call them from another form that is not child of any parent form then they no longer remains child of main parent one obvious reason is that when i intensiate them on that independent form is that I simply cannot use child.MDIParent = this; because it will tend to make independent form parent but i also have tried

MDIParentForm form = new MDIParentForm 

ChildForm child = new ChildForm();
child.IsMdiContainer= form ;
child.Show();

but this also dose not help instead of this it throws an exception that the form that I am trying to set Parent is not MDI Container then to this I give a try and modify

MDIParentForm form = new MDIParentForm ;
form.IsMdiContainer= true;
ChildForm child = new ChildForm();
child.MDIParent = form ;
child.Show();

and in its result nothing appears

Any idea how to..........

Answer

Hans Passant picture Hans Passant · Dec 11, 2010

To create a child from another child, just write it like this:

ChildForm sibling = new ChildForm();
sibling.MdiParent = this.MdiParent;
sibling.Show();

Or fire a custom event that the parent can respond to.