Skip to content

Close the second child form if one form is already open

Ankush

Asked Feb 7, 2016, 8:42 PM EST1 answer
Share
LinkedInX
I have created windows application in which there is a parent form with many child forms. Now what I want to do is, Close the second form if first form is opened. I have menustrip control, that is disabled once any form is opened and enabled if form is closed. Now I have added shortcut keys in menu strip for opening the forms. By using the shortcut I open the first form, then again by using shortcut, the second form is opened and the first form is closed instead of second form.
 The code I wrote :-
 
C#
void MDIParent1_MdiChildActivate(object sender, EventArgs e)
        {
                  if (this.MdiChildren.Count() > 1)
                     {
                     foreach (Form childForm in this.MdiChildren)
                                     {
                                                     if (childForm != this.ActiveMdiChild)
                                                     {
                                                                  childForm.Close();
                                                                  return;
                                                      }
                                     }
                        }
        }
 
 
Thanks in Advance !!!! 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    C#
    private void button1_Click(object sender, EventArgs e)
            {
                FormCollection fc = Application.OpenForms;
                bool FormFound = false;
                foreach (Form frm in fc)
                {
                    if (frm.Name == "Form2")
                    {
                        frm.Focus();
                        FormFound = true;
                    }
                }
                if (FormFound == false)
                {
                    Form2 f = new Form2();
                    f.Show();
                }
            }
    0