469,929 Members | 1,696 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

Disposed form still responds to events

Using vb.net 2008 windows forms.

How can a form which has been closed and disposed still attempt to handle a
custom event?

I have a form which inherits from a base form class.

The base class has a shared event which is raised when a property in the
base class changes (the property updates a shared variable value).

The form handles this event to refresh itself based on the new property
value.

This all works very nicely.

When I close the form I call me.dispose.

If the shared event in the base class is subsequently raised by another form
which inherits the same base class, I get an error in the form which was
already closed and disposed:
"Cannot access a disposed object."
The object refers to a combo box which is populated when the event fires, on
the form which has been closed and disposed.

I work around this by testing the IsDisposed property of the form when the
event fires, but I don't understand why this should be necessary.

Thanks
Bill
Oct 16 '08 #1
2 3734
"BillE" <be****@datamti.comschrieb
Using vb.net 2008 windows forms.

How can a form which has been closed and disposed still attempt to
handle a custom event?
By still being the target of the delegate.

I have a form which inherits from a base form class.

The base class has a shared event which is raised when a property in
the base class changes (the property updates a shared variable
value).

The form handles this event to refresh itself based on the new
property value.

This all works very nicely.

When I close the form I call me.dispose.
If it's not shown modally (ShowDialog), this (me.dispose) is not necessary
because done implicitly.
If the shared event in the base class is subsequently raised by
another form which inherits the same base class, I get an error in
the form which was already closed and disposed:
"Cannot access a disposed object."
The object refers to a combo box which is populated when the event
fires, on the form which has been closed and disposed.

I work around this by testing the IsDisposed property of the form
when the event fires, but I don't understand why this should be
necessary.
I'd remove the handler in the Form's FormClosed event.

Disposing has nothing to do with event handling. The form object can still
handle events. Disposing does not mean killing the form immediatelly. It's
just there to release some ressources ASAP (see the other long long thread
in this group). BTW, as long as the form still handles events, it won't be
collected by the GC.
Armin

Oct 16 '08 #2
Thanks! I understand now.

"Armin Zingler" <az*******@freenet.dewrote in message
news:ew**************@TK2MSFTNGP06.phx.gbl...
"BillE" <be****@datamti.comschrieb
>Using vb.net 2008 windows forms.

How can a form which has been closed and disposed still attempt to
handle a custom event?

By still being the target of the delegate.

>I have a form which inherits from a base form class.

The base class has a shared event which is raised when a property in
the base class changes (the property updates a shared variable
value).

The form handles this event to refresh itself based on the new
property value.

This all works very nicely.

When I close the form I call me.dispose.

If it's not shown modally (ShowDialog), this (me.dispose) is not necessary
because done implicitly.
>If the shared event in the base class is subsequently raised by
another form which inherits the same base class, I get an error in
the form which was already closed and disposed:
"Cannot access a disposed object."
The object refers to a combo box which is populated when the event
fires, on the form which has been closed and disposed.

I work around this by testing the IsDisposed property of the form
when the event fires, but I don't understand why this should be
necessary.

I'd remove the handler in the Form's FormClosed event.

Disposing has nothing to do with event handling. The form object can still
handle events. Disposing does not mean killing the form immediatelly. It's
just there to release some ressources ASAP (see the other long long thread
in this group). BTW, as long as the form still handles events, it won't be
collected by the GC.
Armin

Oct 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by mfdatsw1 | last post: by
9 posts views Thread by David Sworder | last post: by
8 posts views Thread by Tim Geiges | last post: by
5 posts views Thread by theinvisibleGhost | last post: by
4 posts views Thread by wirawan | last post: by
7 posts views Thread by Boni | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.