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 General / February 2005

Tip: Looking for answers? Try searching our database.

Why does the user control's load event fire when components.Dispose() executes?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
moondaddy - 11 Feb 2005 20:58 GMT
I have a user control on a form and when I close the form.

This is the first event that fires:
   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
Then when this line executes the controls Dispose event fires.
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

Now when the line in this event executes:
(components.Dispose())...

   'UserControl overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

The controls load event fires which then executes a bunch of code like
populating controls with data which I don't want since its closing.  It this
correct behavior and what's a good way to trap it in the load event?

Thanks.

Signature

moondaddy@nospam.com

Matt Garven - 12 Feb 2005 03:31 GMT
Put a breakpoint in the handler of the usercontrol's load event
(userControl1_Load or whatever it is called), and examine the callstack. The
Load event is raised when the CreateControl of the UserControl is called.
Find what is causing the call to CreateControl, and see if it is necessary.

If you need further assistance, post the callstack.

A work-around would be to check Disposing before executing the other code in
the load event handler in your user control.

Hope this helps.

Regards,
Matt Garven

> I have a user control on a form and when I close the form.
>
[quoted text clipped - 28 lines]
>
> Thanks.
moondaddy - 12 Feb 2005 22:07 GMT
Thanks Matt,

I already tried using a break point which is why I knew the events list in
my first post were firing and in the order I listed them.  Outside of the
form's "components.Dispose" causing the control's "components.Dispose" to
execute which in turn caused the control's Load event to fire, I an see
nothing else going on.

I did however, find a great solution!  When the for first loads, in the load
event me.parent has a reference to the parent object.  however, when
everything is closing and the "components.Dispose" lines are being executed,
me.parent is nothing.  therefore, I can simply test me.parent as the first
line in the load event:

If Me.Parent Is Nothing Then Return

Works Totaly great.

Signature

moondaddy@nospam.com

> Put a breakpoint in the handler of the usercontrol's load event
> (userControl1_Load or whatever it is called), and examine the callstack.
[quoted text clipped - 47 lines]
>>
>> Thanks.

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.