Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count all open forms of 1 specific class?

I have a few Form-classes that derive from Form. And I want to count all open form instances of 1 special class like Form2. It's a simple WinForms application (no Mdi).

  • Form1 (main)
  • Form2 (can be many)
  • Form3 (other forms)

The app can't be started multiple times. So it's just about counting the windows in this one application.

My ideas:

  • Can Application give me a list of open windows ?
  • WinApi, enumerate windows of 1 application ?
  • put every new Form2 window into a list (I wish to avoid that)
like image 673
Bitterblue Avatar asked Nov 18 '13 13:11

Bitterblue


1 Answers

Use Application.OpenForms property like:

int form2Count = Application.OpenForms.OfType<Form2>().Count();
like image 148
Habib Avatar answered Sep 22 '22 16:09

Habib