Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 9-Apr-14 0:26am
Edited 9-Apr-14 13:34pm
v3

1 solution

Rate this: bad
good
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
    {
        [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:
 
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
  Permalink  
v4
Comments
Salisu Shaibu at 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 at 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 at 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
0 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 10 Apr 2014
Copyright © CodeProject, 1999-2014
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