views:

46

answers:

3

I have a "Switch to window" button on my main form that I'd like to be enabled only when other windows (in my app) are open. Is there some sort of event that is raised whenever a form is opened or closed that my main form can hook? (For example, perhaps some sort of way to track when Application.OpenForms has changed?)

(I understand that if this functionality were in a menu item, I could simply do the Application.OpenForms check when the menu was clicked, but this button is not in a menu.)

+1  A: 

You could define your own type FooForm which inherits from Form. In the constructor of FooForm, fire a statically defined FormOpened event.

Then, all your forms would have a base type of FooForm instead of Form. The event will be fired each time one of your forms is opened.

Philip Wallace
Thank you for the suggestion. I'd have to see if that would be a go at this point; I wanted to make sure I wasn't missing a built-in way to do this.
J Cooper
+1  A: 

I ended up building a window manager class and when a new form is created, it adds itself to the Window Manager's collection. You could encapsulate this functionality in a base Form class so you don't have to remember to do it. Then you can create events in the window manager class to notify you of things like this. You also get the benefit of being able to query the collection of windows in the manager class. I then used this class to be able to consolidate the functionality that builds a menu of open windows into a utility class.

jasonh
Thanks. Given the time, I'd probably go this route, but unfortunately the app wasn't built with this in mind and is quite large and a deadline is looming. I figured I'd ask and see if there was a way without requiring an architecture change :)
J Cooper
+1  A: 

You could use a MessageFilter and monitor for WM_SHOWWINDOW and WM_CLOSE messages.

hjb417