Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a MDI form and two child forms. From one child form if I click add form the other form has to show on the MDI form.

namespace PHTS
{
    public partial class MainForm : Form
    {
        
     
               
        public MainForm()
        {
            InitializeComponent();
        }
		
		Form2 newMDIChild = new Form2
		newMDIChild.MdiParent = this;
       
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form1 newMDIChild = new Form1
newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }
}
         
}



Form1 Add button
private void addbutton_Click(object sender, EventArgs e)
        {
Form2 newMDIChild = new Form2

                newMDIChild.ShowDialog();
             this.Hide();
        }




My problem is form2 is loading outside of the MDIForm. Can anybody knows how to fix this.

Thanks
Posted

Set the MdiParent property to the main form instance - you can get it from the Form1 MdiParent.

However, I would suggest that instead you create an event in your child form which the parent subscribes to, and which gets the parent to create the new child. This means that individual children don't need to know about the existence of others (which is much more in keeping with the OOP idea) and makes it easier to change your overall system should you decide the Mdi is a bad way to go.
 
Share this answer
 
Comments
DominicZA 21-Jul-11 14:51pm    
Great answer!! My 5!
Just to point out the mistake in your code
C#
private void addbutton_Click(object sender, EventArgs e)
{
    Form2 newMDIChild = new Form2();
    newMDIChild.MdiParent = this.MdiParent;
    newMDIChild.Show();
    this.Hide();
}


Otherwise, follow OriginalGriff's advise.
 
Share this answer
 
Comments
[no name] 22-Jul-11 0:43am    
Wondering why my answer was downvoted!!!
Soft009 3-Aug-11 7:24am    
this is the right answer... :) +5

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