Detect UserControl Unloading?

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,462
Programming Experience
10+
We don't have a FormClosing event in a Windows UserControl. How do you detect "unloading" to perform any last minute save routines?
 
HandleDestroyed event looks fine for this.
 
That sounds almost like the disposed event, which concerns me about being too late to do a dataset update if changed are detected.

I wonder if I need to wire in an event so I can check this from the parent form hosting the UserControl, maybe that's a better option.
 
You could declare your own Disposing event. Raising that event would be the first thing you do in the Dispose method, so the sequence of happenings would be like this:

Call Dispose method
Disposing event raised
Object disposed
Disposed event raised

Note that you would have to use a different name, like BeforeDisposed, because there is already a Disposing property.

Having said that, the HandleDestroyed event is raised when the window handle is destroyed. The object cannot and will not be cleaned up until the
HandleDestroyed event handler has completed, so you can be absolutely sure that you can operate on the control normally in the HandleDestroyed event handler. The only thing you cannot do is display the control because at that point it has no window handle. Everything else about it is normal though.
 
I tried HandleDestroyed and I think it's too late in the closing process. I checked for my DataSet.HasChanges and attempted to update the database but it didn't work. I'll have to wire something up so UserControl's can know when a unloading event is occuring to post changes, etc.
 
Back
Top