Window closed event?

P

Paul

In a windows forms app how do you know a window is closing? There seems
to be no event for this.

I have timer object that appears to be firing after the window is closed
(maybe) and I want to shut down and dispose the timer when the window
closes.
 
G

Guest

I would recommend that you create an interface that you require all user
controls to implement. You can then add a method to notify the user control
that it is closing. The UC should then clean up its own resources. Within
the tree control you can also raise an event notifying outside listeners that
the view is changing. Finally you should consider simply hiding the UC when
it switches away instead of closing it. Some views may be very costly to
create and therefore you could leave it up to the view itself to decide
whether it should close or simply hide itself. It depends on how often the
view is likely to change (for example in Explorer the file view is normally
the only view seen so unloading and reloading it each time the user changes
their selection is overkill).

As a side note since you only have a single view on the right side you can
simply hide the view when the user selects another node. In the
OnVisibleChanged event of your UC you could then do your cleanup (or at least
toggle it on or off). This might not work well if your app minimizes but
trial and error would reveal the issues.

Michael Taylor - 9/19/05
 
P

Paul

TaylorMichaelL said:
I would recommend that you create an interface that you require all user
controls to implement. You can then add a method to notify the user control
that it is closing. The UC should then clean up its own resources. Within
the tree control you can also raise an event notifying outside listeners that
the view is changing. Finally you should consider simply hiding the UC when
it switches away instead of closing it. Some views may be very costly to
create and therefore you could leave it up to the view itself to decide
whether it should close or simply hide itself. It depends on how often the
view is likely to change (for example in Explorer the file view is normally
the only view seen so unloading and reloading it each time the user changes
their selection is overkill).

As a side note since you only have a single view on the right side you can
simply hide the view when the user selects another node. In the
OnVisibleChanged event of your UC you could then do your cleanup (or at least
toggle it on or off). This might not work well if your app minimizes but
trial and error would reveal the issues.

I like your ideas and as I have more time I will try them out. I do have
a UserControl base class to provide a standard template for the views.
My app is pretty trivial at this point and I am now experimenting with
the following. Whenever the tree node changes (new view) do this first
before changing views:

if(currentView != null)
{
panel1.Controls.Remove(currentView);
currentView.Dispose();
GC.Collect();
}

It seems to be doing what I want -- completely destroy the current view.
I like the idea of only creating the views once, and perhaps I can store
the view object in the Tag property of the tree node. If the Tag != null
then just load the view object, otherwise create it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top