Click here to Skip to main content
16,018,202 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am a beginner on C# .NET, trying to code out something. I have three Windows.Form:

1.	MainForm (MDI_FORM)
2.	Child1
3.	Child2

On MainForm, I commanded Child1.Show()
Then from Child1, I intend to assign and open Child2 as a child to MainForm via the codes below:

    On MainForm, I declared:

         public static Child2 child2;


    Then on btnOpen from Child1, I coded:

         private void btnOpen_Click(object sender, EventArgs e)
            {
                MainForm mainForm = new MainForm();
                mainForm.child2 = new Child2 ();
                mainForm.child2.MdiParent = fMainForm;
                mainForm.child2.Show();
            }

Someone help me on what to do please.
It's not working.
Thanks a lot!!!
Posted
Updated 9-Apr-14 12:34pm
v3

1 solution

Hi Salisu Shaibu,

Your code doesn't work because you are creating a new instance of your MainForm. The trick is to use a reference to the MainForm and set it as MdiParent to your second Form. Have a look at this (runnable) example:

C#
using System;
using System.Windows.Forms;

namespace MdiOhMy
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Form formMain = new Form { IsMdiContainer = true };

            Form formChild1 = new Form { Text = "Child1", MdiParent = formMain, Visible = true };
            Button buttonShowChild2 = new Button { Text = "Open Child2", Parent = formChild1 };
            buttonShowChild2.Click += (object sender, EventArgs e) =>
                {
                    Form formChild2 = new Form { Text = "Child2", MdiParent = formMain, Visible = true };
                };

            Application.Run(formMain);
        }
    }
}


In this example I can reference the formMain instance because it's in the captured scope. In a more realistic Scenario you can obtain a reference via Child1's MdiParent property.

So your handler could look like this:

C#
private void btnOpen_Click(object sender, EventArgs e)
 {
     MainForm mainForm = this.MdiParent as MainForm; // "this" is Child1 which refernces it's MdiParent
     MainForm.child2 = new Child2();
     MainForm.child2.MdiParent = fMainForm;
     MainForm.child2.Show();
 }



Also think about if you want to create a new instance of Child2 every time you click your button, or just hide/reshow the same instance...

Happy coding

Johannes
 
Share this answer
 
v4
Comments
Salisu Shaibu 9-Apr-14 18:30pm    
Thanks very much Johannesnestler, I removed all the codes on my Program class and edited yours on it then, placed the btnOpen_Click as you'd advised.

My only problem now is MainForm.child2 = new this.MdiParent as MainForm;

Its displaying error.
Just a little more correction please.
U're appreciated.
johannesnestler 10-Apr-14 4:40am    
Hi salisu,

Your Problem seems to be that you want to set your Child2 Form to a MainForm - but "new" Hmm?. I think ist maybe just a typo, otherwise it seems you have some Ssntax understanding problems, maybe have a look at your C# book again ;-)
So first obtain instance to MainForm (through Child1's MdiParent property)
I don't know if you are creating the Child2 Form still "inside" the Child1 form (so "this" should be Child1)
MainForm mainform = this.MdiParent as MainForm; //assuming MdiParent is of type MainForm
then Create (or reshow) your Child2 instance and assign it to your child2 property
mainform.child2 = new Child2();
Hard to tell why you do all this (if you have a property on MainForm for your Child2 Form this doesn't seem to give very "dynamic" behaviour). Maybe think again when to instantiate the Form (maybe during construction of the MainForm), and when you want to Show/hide it - maybe there is a better solution. I'm wondering because normally you would create a dialog like this, and not a Form for which you are holding a special reference (through MainForms child2 property)
johannesnestler 10-Apr-14 4:42am    
Oh sorry the typo was mine - I corrected it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900