Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 forms i.e form1,form2 & form3.
and set ismdicontainer is true in form1.

form1 button click for form2 open

// // Form1.IsMdiContainer should be true
            //Form1 form1 = new Form1();
            // This automatically adds form2 into form1's MdiChildren collection
            Form2 form2 = new Form2();
            form2.MdiParent = this;
            form2.Show();


now when i want to open form3 child in form1 mdiparent.below i tried code but not solved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mdi_3Forms
{
    public partial class Form2 : Form
    {
        // Include as data member so we only instantiate one Form3
        Form3 _form3;
        Form1 form1;
        public Form2()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_form3 == null)
            {
                this.Close();
                _form3 = new Form3();
                // Set Form3's parent to be Form1
                _form3.MdiParent = this.form1;
                _form3.Show();
            }

        }
    }
}


form2 closed but form3 is not set mdi child in form1.
pls help with solution.

sorry for my bad english.
many thanks.

What I have tried:

Form1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mdi_3Forms
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            
            InitializeComponent();
           
        }
         

        private void button1_Click(object sender, EventArgs e)
        {
           // // Form1.IsMdiContainer should be true
            //Form1 form1 = new Form1();
            // This automatically adds form2 into form1's MdiChildren collection
            Form2 form2 = new Form2();
            form2.MdiParent = this;
            form2.Show();
           
        }
    }


Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mdi_3Forms
{
    public partial class Form2 : Form
    {
        // Include as data member so we only instantiate one Form3
        Form3 _form3;
        Form1 form1;
        public Form2()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_form3 == null)
            {
                this.Close();
                _form3 = new Form3();
                // Set Form3's parent to be Form1
                _form3.MdiParent = this.form1;
                _form3.Show();
            }

        }
    }
}
Posted
Updated 4-Apr-20 5:36am
Comments
Maciej Los 4-Apr-20 11:17am    
Please, do NOT repost!
C# mdi between three forms[^]

The whole idea is that you don't do it that way: Form2 shouldn't know Form1 exists, much less Form3, let alone be responsible for displaying it!
Instead, Form2 should talk to Form1, tell it it has a user request and let Form1 handle what it does with it: that could be a MessageBox, or it could open a new Form2, or a Form3; or it could activate an existing Form3 that Form2 knows nothing about - that's up to the parent form, not the child.

That sounds complicated, but it isn't, not really. Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] and it will show you how, and even give you sample code you can adapt to your specific needs.
 
Share this answer
 
v2
Comments
Maciej Los 4-Apr-20 11:20am    
5ed!
Seems, you just want to open child form in this schema:
Form1 has to be a main form. Form2, Form3, etc.has to be a child form.
The order of opening forms is:
Form1 -> Form2 -> Form3

Every child form constructor has to be changed to:
C#
public Form2(Form ParentForm)
{
    InitializeComponent();
    this.MdiParent = ParentForm;
}


public Form3(Form ParentForm)
{
    InitializeComponent();
    this.MdiParent = ParentForm;
}


Usage:

C#
//in Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(this);    //refers to Form1
    form2.Show();
}

//in Form2
private void button1_Click(object sender, EventArgs e)
{
    Form3 form3 = new Form3(this.MdiParent); //refers to Form2.MdiParent, which is Form1
    form3.Show();
}
 
Share this answer
 

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