Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have form1,form2 and form3.form1 is my main mdi form.and also property set to ismdicontainer is true.
form1 buton click form 2 open
below code.
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
    }
}


if form 2 button click i want to open form3 in form1 with mdiparent and form2 will be close.
here is my code form 2 button click code
private void button1_Click(object sender, EventArgs e)
       {
           this.Hide();
           Form3 f3 = new Form3();
           f3.MdiParent = this.MdiParent;
           f3.Show();

           this.Close();

       }


but its not working pls help.

What I have tried:

<pre> private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form3 f3 = new Form3();
            f3.MdiParent = this.MdiParent;
            f3.Show();
            
            this.Close();

        }
Posted
Updated 4-Apr-20 5:37am
Comments
[no name] 17-Mar-20 13:45pm    
Because Form2 has no MdiParent
Maciej Los 17-Mar-20 14:41pm    
Sounds like an answer, Bruno.
Member 10570811 17-Mar-20 13:51pm    
also i have set form 2 property ismdicontainer is true.but not solve.
pls help me with correct code.
Maciej Los 17-Mar-20 14:33pm    
Use "Reply" widget to be sure that system will inform user about your reply.
Dave Kreskowiak 17-Mar-20 23:26pm    
You cannot have an MdiContainer form inside another MdiContainer form. If you're trying to have both Form1 and Form2 as MdiContainers, Form2 can never be a MdiChild of Form1.

Form2 shouldn't know about Form3, or even about Form1.
Instead, Form2 sends a message to it's parent - which happens to be Form1 - and it decides what happens as a result. This happens via Events, just like everything else in .NET does. (A simplification, I know).

See here: Transferring information between two forms, Part 3: Child to Child[^] - it gives teh general principles.
 
Share this answer
 
Comments
Member 10570811 17-Mar-20 13:51pm    
pls help me with code.thai i can understand easily.
OriginalGriff 17-Mar-20 14:41pm    
Follow the link - it explains and gives code.
Member 10570811 18-Mar-20 9:00am    
Iam tried but could not understand.pls help with solution code so i can move forword.iam stuck here
OriginalGriff 18-Mar-20 9:07am    
What part of it is giving you difficulties - I have no idea where you are in your course, what you do or don't know how to do - so you have to tell us.
Member 10570811 18-Mar-20 11:28am    
i have 3 forms.
form1 is main form i.e parent form and ismdicontainer propery set to true in form1.

//here is form 1 button click for form 2 open
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
now i want to form 2 button click then form2 close and form3 open a child in to form1 parent.

trying below code

// form2 button click for form3 open and form 2 close
private void button1_Click(object sender, EventArgs e)
{
//hide form2
this.Hide();
//open form 3
Form3 f3 = new Form3();
f3.MdiParent = Form1.ActiveForm;
f3.Show();
//close form2
this.Close();

}

form 2 closed but form3 not open as a child form3 to parent form1.

please share correct code.its helpful for me.
sorry my bad launguage.
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