locked
[MDI FORMS] Opening a child Form from another form which is not a child form. RRS feed

  • Question

  • Hi have a problem with regards to MDI form
    I have a MDI form lets call it MainForm

    It has 2 Child froms, lets call them Child1 and Child2

    On child2 there is a button that will trigger a custom dialog
    which is still a form with an OK and Cancel Button

    if i click on OK on that custom dialog, i want to open up the Child1 under the MDI form.

    Here is my code when calling an ChildForm from MDI form or Main Form

                if (_PurchaseOrderHistory == null)
                {
                    
                    _PurchaseOrderHistory = new frmPurchaseOrderHistory
                    {
                        MdiParent = this
                    };
                    _PurchaseOrderHistory.Show();
                    _PurchaseOrderHistory.WindowState = FormWindowState.Maximized;
                }
                else
                {
                    _PurchaseOrderHistory.Activate();
                }

    Here is what i use when calling ChildForm from another ChildForm

     _purchaseOrderAdd = new frmPurchaseOrderAdd 
    { MdiParent = ParentForm };
    
    _purchaseOrderAdd .Show

    Now, how do i call a child form from a form which is not a childform.

    Thank you




    • Edited by Dikong42 Friday, September 30, 2016 1:20 PM
    • Moved by CoolDadTx Friday, September 30, 2016 2:42 PM Winforms related
    Friday, September 30, 2016 12:43 PM

Answers

  • Hi,

    What important is pass the ref of MainForm, see the code below.

    [in MainForm]

           private void MainForm_Load(object sender, EventArgs e)
            {
                ChildForm2 f2 = new ChildForm2(this);
                f2.MdiParent = this;
                f2.Show();
            }

    [In ChildForm2]

            private MainForm mainform = null;
            public ChildForm2(MainForm mf)
            {
                InitializeComponent();
                mainform = mf;
            }
            private void button1_Click(object sender, EventArgs e)
            {
               if( MessageBox.Show("Open Child Form2 ?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    ChildForm1 f1 = new ChildForm1(mainform);
                    f1.MdiParent = mainform;
                    f1.Show();
                }
                
            }
    [In ChildForm1]
            private MainForm mainform = null;
            public ChildForm1(MainForm mf )
            {
                InitializeComponent();
                mainform = mf;
            }

    Best Regards,

    Wednesday, October 5, 2016 5:31 AM

All replies

  • Now, how do i call a child form what a form which is not a childform.

    Thank you

    Hi,

    instanciate those Forms with a reference to the mdiMainForm:

    In the Form thats not a MdiForm, add a constructor that takes a reference to the MdiForm as a Parameter, like

    [In Child2]

            private Form1 _mdiForm = null;
    
            public FormChild2(Form1 mdiForm) //Form1 here is the MDIMainForm
            {
                InitializeComponent();
                _mdiForm = mdiForm;
            }

    now you can instanciate that form with the parameter in your mdiMainForm:

    [in MainMdiForm]

    FormChild2 ch2 = new FormChild2(this);

    and when you want to add a mdiChild from the non MdiChildForm (Child2) you can set the _mdiForm as MdiParent

    [in Child2]

    MdiParent = _mdiParent;

    Regards,

      Thorsten


    Friday, September 30, 2016 12:59 PM
  • On child2, i will open up a form which is not a childform. It is a small form that acts as a dialog. That form has two buttons, and if i will click on a button i want to open up child1.. I can open child1 but it is not open as a childform now.
    Friday, September 30, 2016 1:25 PM
  • Hi,

    What important is pass the ref of MainForm, see the code below.

    [in MainForm]

           private void MainForm_Load(object sender, EventArgs e)
            {
                ChildForm2 f2 = new ChildForm2(this);
                f2.MdiParent = this;
                f2.Show();
            }

    [In ChildForm2]

            private MainForm mainform = null;
            public ChildForm2(MainForm mf)
            {
                InitializeComponent();
                mainform = mf;
            }
            private void button1_Click(object sender, EventArgs e)
            {
               if( MessageBox.Show("Open Child Form2 ?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    ChildForm1 f1 = new ChildForm1(mainform);
                    f1.MdiParent = mainform;
                    f1.Show();
                }
                
            }
    [In ChildForm1]
            private MainForm mainform = null;
            public ChildForm1(MainForm mf )
            {
                InitializeComponent();
                mainform = mf;
            }

    Best Regards,

    Wednesday, October 5, 2016 5:31 AM