3

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..........

Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

4 Answers4

8

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • ok this works fine but what if the form from you are going to call the child dose not have any parent lets say from a Parent there is a pop up message that is not child of that form how then what scheme should be adopted – Afnan Bashir Dec 12 '10 at 14:00
  • Lots of ways. Passing the reference to the parent through the form's constructor is an obvious way. Even Application.OpenForms[0] would work. Or giving the parent a static property, there should be only one MDI parent. Letting the parent take care of creating children is the best way. That custom event I mentioned. Or the ShowDialog() return value. – Hans Passant Dec 12 '10 at 14:10
0

write this code in a parent form....

childform  obj = new childform( );
               obj.MdiParent = this;
               obj.StartPosition = FormStartPosition.CenterScreen;
               obj.Show( );
0

You should set the Parent to be the already existing mdiform, not create a new one.

If there isn't an instance of the mdiform already, you should not only create an instance of the form, but also show it.

var mdiForm = new MdiForm();
mdiForm.IsMdiContainer = true;
var childForm = new ChildForm();
childForm.MdiParent = mdiForm;
mdiForm.Show();
childForm.Show();

Also notice that I use mdiForm.IsMdiContainer, AFAIK there is no IsMdiParent property.

comecme
  • 6,086
  • 10
  • 39
  • 67
  • Yes I was wrong at the property name.I tried to do your code but did not helped me.instead it showed me a new form dialogue having double control boxes – Afnan Bashir Dec 12 '10 at 12:06
  • My code is showing a new form because the code creates a new instance of the MdiForm. If an instance of the MdiForm already exists, you should set the Parent property to the already existing MdiForm, like I said in the first sentence. – comecme Dec 12 '10 at 15:30
0
class MainClass
{
   public string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
   public void showWindow(Form openWin, Form closeWin, Form MDI)
    {
        closeWin.Close();
        openWin.WindowState = FormWindowState.Minimized;
        openWin.MdiParent = MDI;
        openWin.Show();
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225