Click here to Skip to main content
13,091,369 members (54,527 online)
Rate this:
Please Sign up or sign in to 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;
Someone help me on what to do please.
It's not working.
Thanks a lot!!!
Posted 8-Apr-14 23:26pm
Updated 9-Apr-14 12:34pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:

using System;
using System.Windows.Forms;
namespace MdiOhMy
    static class Program
        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 };

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:

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;

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

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 10 Apr 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100