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

M

moondaddy

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.
 
G

Guest

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
 
M

moondaddy

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.
 

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