Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a windows form is already open, and close it if it is?

Tags:

c#

winforms

I have a form "fm" that is a simple info window that opens every 10 mins (fm.Show();).

How I can make that every 10 mins it will check if the form "fm" is open and if it is open it closes it and open it again!

Now the form fm is always created with form fm = new form();
so when I try to check if the form is open it will always be false and open a new window even if there is one form before!

I need to have a tool to give it a unique identity and then check if this form with unique identity is opened or not!

I do not want to just update the data on the form (fm), because I have a complicated info with buttons.

The form name is "UpdateWindow"

Thanks

like image 555
Data-Base Avatar asked Oct 05 '10 07:10

Data-Base


People also ask

How do you check if form is already open in VB net?

For more simplicity you may create a public static bool variable which will tell whether the form is opened or not. On form load event assign 'true' and on closed event assign 'false' value. Show activity on this post. Show activity on this post.

How check form is loaded or not in C#?

void Click() { var tempFrm = new dialogForm(); tempFrm. Load += frmLoad; tempFrm. Show(); } void frmLoad(object s, EventArgs ea) { // form loaded continue your code here! }

How do I close my WinForms form?

When we need to exit or close opened form then we should use "this. Close( )" method to close the form on some button click event. When we are running a winform application & need to exit or close SUB APPLICATION or CURRENT THREAD then we should use "System.

How do I stop a windows form from closing?

To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true .


1 Answers

maybe this helps:

FormCollection fc = Application.OpenForms;  foreach (Form frm in fc) { //iterate through      if (frm.Name == "YourFormName")      {          bFormNameOpen = true;      } } 

Some code in the foreach to detect the specific form and it could be done. Untested though.

Found on http://bytes.com/topic/c-sharp/answers/591308-iterating-all-open-forms

like image 167
Sascha Avatar answered Oct 06 '22 06:10

Sascha