Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have attached many child form to a parent form but here i want when i open a child form other opened child form should be closed in c# pls help me out with this.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Mar-14 3:28am    
If this is not MDI, I bet none of these forms are children to any other form. Anyway, what's the problem? Open or close whatever you want...
—SA
BillWoodruff 11-Mar-14 4:10am    
Please tag your question to indicate if this is an MDI Application.

1 solution

try this it will solve your issue

C#
if (ActiveMdiChild != null)
    ActiveMdiChild.Close();



Ex:

suppose i have a MDIParent form and some child forms then on the click event of menu item the code will be as follows

C#
private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
      {
          if (ActiveMdiChild != null)
             {
               ActiveMdiChild.Close();
               ChildForm2 ch2 = new ChildForm2();
               ch2 .MdiParent = this;
               ch2 .Show();
             }

      }   

and if u want to check out the name of the current Active form name before closing it then the following code will help you out :

C#
private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (ActiveMdiChild != null)
           {

               if (ActiveMdiChild.GetType().Name != "ChildForm2")
               {
                   ActiveMdiChild.Close();
                   ChildForm2 ch2= new ChildForm2();
                   ch2.MdiParent = this;
                   ch2.Show();
               }
               else
               {
                   MessageBox.Show("Form is already open");
               }

           }
           else
           {
               ChildForm2 ch2= new ChildForm2();
               ch2.MdiParent = this;
               ch2.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