Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if form is open, if open close form?

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;
 }
like image 638
Edwin Torres Avatar asked Nov 18 '12 22:11

Edwin Torres


1 Answers

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");
like image 59
Sergey Berezovskiy Avatar answered Oct 11 '22 14:10

Sergey Berezovskiy