5

How do I check if a form is open, and if it is open to close the form?

I tried the following, testing out some code but it keep saying the form is not open even when I know it is:

 foreach(Form a in Application.OpenForms) 
 {
     if (a is YouLikeHits_Settings) 
     {
         // About form is open
         MessageBox.Show("form open");
         break;
     }
     // About form is not open...
     MessageBox.Show("form not open");
     break;
 }
default locale
  • 13,035
  • 13
  • 56
  • 62
Edwin Torres
  • 493
  • 3
  • 7
  • 25

3 Answers3

20

Application.OpenForms contains opened forms. If form in this collection, then it is opened. Otherwise it is not opened (possibly closed).

if (Application.OpenForms.OfType<YouLikeHits_Settings>().Any())
    MessageBox.Show("Form is opened");
else
    MessageBox.Show("Form is not opened");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Was I did was make a button which opens the form, then in another button I put in the code above, when I click it, it keeps saying, form is not open. – Edwin Torres Nov 18 '12 at 22:14
  • 1
    Your code worked to detect it being open :). Any way after it pops up with the message box that the form is open I can close that certain form? – Edwin Torres Nov 18 '12 at 22:16
  • 1
    Yes, you can: `Application.OpenForms.OfType().First().Close()` – Sergey Berezovskiy Nov 18 '12 at 22:18
  • i have tried it but my certainly form open cannot be closed automaticly – aminvincent Feb 17 '16 at 10:54
  • @SergeyBerezovskiy i have same problem like this.. i tried your suggest code and it ran well when i show my form using `form1.show();` but when i tried use `form1.showdialog();` the form cannot be closed. if any suggest for me i will say thanks so much – aminvincent Feb 18 '16 at 01:29
  • @aminvincent I think you should create new question with all related to your problem information and code to reproduce issue. – Sergey Berezovskiy Feb 18 '16 at 07:26
  • @SergeyBerezovskiy thanks for your suggest,..finally i create new question and this the link that i created and that it solved: http://stackoverflow.com/questions/35471376/close-messagebox-when-incoming-new-message/35471810?noredirect=1#comment58640277_35471810 – aminvincent Feb 19 '16 at 08:07
  • Many thanks, it worked like a charm and exactly what you said and I needed :) – Juano Jun 26 '20 at 06:05
1

This will work sure

            if (Application.OpenForms.OfType<frm_YouLikeHits_Settings>().Any())
            {
                Application.OpenForms.OfType<frm_YouLikeHits_Settings>().First().Close();
            }
            frm_YouLikeHits_Settings f1= new frm_YouLikeHits_Settings();
            f1.MdiParent = this;
            f1.Show();
Nitin...
  • 349
  • 3
  • 7
0
try
{
    if (Application.OpenForms.OfType<talkForm>().Any())
    {
        talkForm frm = new talkForm();
        frm.Close();
        MessageBox.Show("Form is opened");
    }
    else
    {
        talkForm frm = new talkForm();
        frm.Show();
        MessageBox.Show("Form is not opened");
    }
}
catch(Exception ex)
{

}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
John G
  • 319
  • 4
  • 8