Search code examples
c#autofacmdi

MDI child form not staying open


I am trying to get a MDI child form to show when I click on a menu strip item

MDIParent1 parent = new MDIParent1();
public void NewForm(object sender, System.EventArgs e)
{
    Form3 childForm = new Form3();
    childForm.Show();
    childForm.MdiParent = parent.Parent;
    childForm.StartPosition = FormStartPosition.CenterParent;
}

The form is in its own Class Library if I don't set the MDIParent the form loads up and stays up when i set the parent it shows for a split second then it's gone.

I am using AutoFac to build my menu items based on Dynamic menu creation IoC but unable to get this to work. I have this in my main form which should then get the correct MDIParent in.

public MDIParent1 Parent
{
   get 
     { 
        return this; 
     }
}

Hope someone can figure out where I am going wrong.

Aidan


Solution

  • Figured it out after some trial and error.

    in the IMenuContainer I had to add

    MDIParent1 Parent { get; }
    

    And then in my ClassMenuBuilder I had to

     MDIParent1 parent = new MDIParent1();
     public void BuildMenu(IMenuContainer container)
     {
        parent = container.Parent;
     } 
    
     public void NewForm(object sender, System.EventArgs e)
     {
        Form3 childForm = new Form3();
        childForm.MdiParent = parent.Parent;
        childForm.StartPosition = FormStartPosition.CenterParent;
        childForm.Show();
     }
    

    Now when I click the Form3 open button it opens the form in the MDIParent.

    Hope this helps someone else out.

    Aidan