Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close all open WinForms but one?

Tags:

.net

winforms

I am working with a windows forms application, .NET 3.5. I have set a timer such that if there is no activity after a certain period the current form is closed and the user is returned to a default "not logged in" form, which displays certain information but does not allow access to other forms.

Application.OpenForms gives me a list which allows me to close most forms that might be left open, but Application.OpenForms seems to be aware of only the MDI forms. Some of these MDI forms may have MessageBoxes or other modal forms. There may even be cases where the modal forms themselves have popup dialogs.

I am aware of the quirk (bug) in Application.OpenForms, I don't believe there are any cases where properties are being altered after a Messagebox or other similar form has been created. In any case, that should effect only those forms, all others should still be listed in ApplicationOpenForms.

Is there an elegant way to iterate through and close every form of whatever type in the application except the one I care about? (Or strictly, to close them all but the MDI Parent then open the one I care about.)


2 Answers

can you consider registering the handle/reference of the form with a data structure (like an array defined as a singleton) as soon as its created.

With this approach you will have all the forms created and potentially can even store other information of interest on the same structure. Once you have the list, you can run a worker process in a second thread to do the cleanup based on the information stored in the data structure

like image 184
Chintana Meegamarachchi Avatar answered Nov 27 '25 19:11

Chintana Meegamarachchi


This is a 98% answer for me, perhaps 100% for others.

In almost every case, the forms that were giving trouble were simply notifications or confirmations i.e., "Record Saved", or "Do you really want to cancel?" and were implemented using MessageBox. All that was needed was to create my own form that was functionally equivalent to MessageBox, create an instance of that in the MDI child that needed it, and use it instead. When the MDI child form closed (found by iterating through Application.OpenForms), the modal confirmation form closed as well. I imagine that the reason MessageBox was not closing was because it was not actually owned by the MDI child form. (MSDN : "You cannot create a new instance of the MessageBox class.")

Of course I did not need to replicate the entire MessageBox functionality, in most cases all I really needed was a textbox and a couple of buttons.

Other built-in Windows forms, such as PrintDialog and FileOpenDialog have issues similar to MessageBox. I have not gone to the trouble to recreate my own versions of these. If one is accidentally left open no great harm is done. This will only happen if someone walks away between bringing up PrintDialog and actually hitting the Print button on the PrintDialog form, then does not come back before the inactivity timer expires.

There is probably a relatively simple way to send a message to these types of forms to tell them to close - worst case, look them up and use low level windows messaging.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!