Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2007

Tip: Looking for answers? Try searching our database.

Unload Event For User Control?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Gaines - 27 Jan 2007 11:15 GMT
I want to save some data when my User Control unloads but I can't find an
UnLoad event.

Is there such a thing? If not is there another way of trapping when it
unloads?

Signature

Jeff Gaines

Jeffrey Tan[MSFT] - 29 Jan 2007 09:53 GMT
Hi Jeff,

Yes, only Asp.net System.Web.UI.Control class has Unload event. .Net
Winform controls do not have Unload event.

In .Net Winform, the Form class has the "Closing" event("FormClosing" event
in .Net2.0) which is fired when the form is in the process of closing. So,
a general solution for your task is using this.FindForm() method to get the
main form reference, which the UserControl resides in; then you may
register a handler for the form's Closing/FormClosing event; in the
handler, you may save the usercontrol data.

Another solution is placing the code in UserControl.Dispose() method.
Normally, this method will be called when the form is closing, however,
since I did not do the complete analysis of this method, I am not sure if
there is any situations that this method is not called immediately, so I
stilll recommend the first solution of registerring form's
Closing/FormClosing event in UserControl.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jeff Gaines - 29 Jan 2007 10:35 GMT
On 29/01/2007 in message <DmjGWt4QHHA.1176@TK2MSFTNGHUB02.phx.gbl>

>Yes, only Asp.net System.Web.UI.Control class has Unload event. .Net
>Winform controls do not have Unload event.

Oh dear, we're the poor relations again :-)

Many thanks for that input Jeffrey, it gives me a couple of options at
least!

Signature

Jeff Gaines

Jeffrey Tan[MSFT] - 29 Jan 2007 15:32 GMT
Hi Jeff ,

Thanks for the feedback.

Yes, since the Asp.net BCL and Winform BCL are developed by different
teams, they choose different event model for its control model. I think
this makes sense, because Asp.net implements its server-side model itself,
while Winform encapsulates Win32 GUI controls model, their control model
may not always be identical.

Although Microsoft designs them with similar Control names so that
developers can quickly be familiar with the control model when moving from
one to the other. However, this will also introduce some confusion to
developers when switching between Asp.net and Winform. The best solution is
always consulting the MSDN to know without a doubt. :-)

Anyway, if you still need further help or have any concern, please feel
free to feedback. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Serge Baltic - 29 Jan 2007 12:51 GMT
Hello,

> In .Net Winform, the Form class has the "Closing" event("FormClosing"
> event in .Net2.0) which is fired when the form is in the process of
> closing. So, a general solution for your task is using this.FindForm()
> method to get the main form reference, which the UserControl resides
> in; then you may register a handler for the form's Closing/FormClosing
> event; in the handler, you may save the usercontrol data.

What happens to FindForm if the control has no parent form at all? Null reference,
I guess. I'd rather use a self-contained solution to the problem. For example,
the HandleDestroyed event on the control will fire when the control window
is being destroyed (WM_DESTROY message). If this is your own control, override
the OnHandleDestroyed method instead of sinking the event (don't forget to
call the base implementation).

In some scenarios, OnVisibleChanged method / VisibleChanged event may also
be useful, because it may trigger earlier than HandleDestroyed and closer
to the moment when the user thinks that “the control has been unloaded”.

(H) Serge

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.