Usercontrol open mdichild?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kate Giacomozzi
    New Member
    • Nov 2011
    • 5

    Usercontrol open mdichild?

    I am bulding a Windows Form Application in Visual Studio using C#.

    In my application the main form (form1) has a usercontrol with a button. When the user clicks the button I would like a MdiChild form (of form1) to appear.

    Normally when opening a MdiChild from a button on a parent form or another child form I use:

    Code:
    private void Button1_Click(object sender, EventArgs e)
     {
     
    Form2 newMdiChild = new Form2();
     newMdiChild.Mdiparent = this.Mdiparent;
     newMdiChild.Show();
    However, this does not work with the Usercontrol button due to an error that states the Usercontrol does not contain a definition for MdiParent.

    I am still new to programming but any help would be greatly appreciated.
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    is this Example solve your problems?

    create 3 Forms: Form1, Form2 and Form3.

    Form1:
    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.IsMdiContainer = true;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                f2.Show();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form3 f3 = new Form3();
                f3.MdiParent = this;
                f3.Show();
            }
        }
    }
    Form2:
    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.MdiParent = this.MdiParent;
                f2.Show();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form3 f3 = new Form3();
                f3.MdiParent = this.MdiParent;
                f3.Show();
            }
        }
    }
    Form3:
    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form3 f3 = new Form3();
                f3.MdiParent = this.MdiParent;
                f3.Show();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.MdiParent = this.MdiParent;
                f2.Show();
            }
        }
    }

    Comment

    Working...