0

I am trying to open ChildForms on MdiParent Form using a Form Manager Class. I've tried several alternatives in order to tell "FormManager" Class which Form is the MdiParent; however it would always throw errors (1 different error for each different occasion). Most of the times it would say that MainForm was not a MdiContainer; and this is not true as I had set MainForm previously as such.

Note:1 Main Form is set as a MdiContainer.

Note:2 If I attempt to open only 1 Form it will open perfectly inside MdiParent Form. However if I attempt to Load 2 or more Forms it will throw error.

Note:3 If I create the Child Forms Instances on Main Form; Everything works just fine. No issues at all. But this is not how I want to do it.

  // [Class] : Main Form
  // Member Variables
  private FormManager FrmMgr;

  // Load Forms using FormManager Class
  private void Load_Forms()
  {
  // Create a new Instance of FormManager.
      if (FrmMgr == null) { FrmMgr = new FormManager(); }
  // Set MainForm as MdiParent in FormManager Class
      FrmMgr.MdiParent = this;
  // Alternatively Manually Set the MdiParent in MainForm (Uncomment Line Bellow)
  // this.IsMdiContainer = true;
  }


  // [Class] : Form Manager
  // <Member Variables>
  private Form1 frm1;
  private Form2 frm2;

  // FormManager <Constructor>.
  // Constructor Receives MdiParent Information from MainForm
  // and Sets the MdiParent.ActiveControl Information to Every Form
  // when calling each (Form initiation) Method.
   public FormManager(Form _MainParent)
   {
       // Create new Instance of Form1
       Init_Frm1(_MainParent);

       // Create new Instance of Form2
       Init_Frm2(_MainParent);
   }

    private void Init_frm1(_MainParent)
    {
        if (frm1 == null) { frm1 = new Form1(); }
        frm1.MdiParent = _MainParent;

        frm1.Show();
    }

    private void Init_frm2(_MainParent)
    {
        if (frm2 == null) { frm2 = new Form2(); }
        frm2.MdiParent = _MainParent;

        frm2.Show();
    }
}

Can anyone point me in the right direction and help me understand why this happens? Thanks in advance.

2
  • 1
    I didn't understand quite well. This variable form1 and this procedure Init_form1() are used just for the first form or do you intended to use them for all child forms? If this is the code for the first form, how is the code for the second form (since it's when you are having trouble)? Mar 24, 2018 at 19:01
  • Greetings Pedro; Init_Form1() Method is inside my FormManager Class. This method contains the code that will create a new instance of this form. The forms are ran within the FormManager Constructor. In other words: Whenever I create the Instance of FormManager Class; it's constructor will run the methods inside. In this case will be the methods to run the ChildForms.
    – user2225495
    Mar 24, 2018 at 19:47

1 Answer 1

0

I was finally able to sort it out.

The issue seemed to be consisted in the way I've used to pass "MainForm" as the "MdiParent" to "FormManager" class.

// [Class] MainForm
// Create new Instance (Always Active); of FormManager Class
// Note: When FormManager Class is Loaded; it's Constructor will
// run the Methods that will open all the initial Forms.

private void Load_Modules()
{
    // "this" refers to "MainForm"
    if (FrmMgr == null) { FrmMgr = new FormManager(this); }
}


// [Class] FormManager
// Member Variables
private Form1 frm1;
private Form2 frm2;

// <Constructor>
public FormManager(Form _MainParent)
{
    // Create new Instance of Form1 under "Init_Frm1" Method
    Init_Frm1(_MainParent);

    // Create new Instance of Form2 under "Init_Frm2" Method
    Init_Frm2(_MainParent);
}

// Method that Will Create new Instance of Form1
private void Init_Frm1(Form _MainParent)
{
    if (frm1 == null) { frm1 = new Form1(); }
    frm1.MdiParent = _MainParent;

    frm1.Show();
}

// Method that Will Create new Instance of Form2
private void Init_Frm2(Form _MainParent)
{
    if (frm2 == null) { frm2 = new Form2(); }
    frm2.MdiParent = _MainParent;

    frm2.Show();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.