Unsubscribe to events

M

MAF

I have an event on a sub form in my application and I want to unsubscribe
all subsribers when I close the form, is there a way to do this?

private void frmDetail_FormClosing(object sender, FormClosingEventArgs e)

{

if (OnDetailChanged != null)

{

???????

}

}



Thanks
 
M

Marc Gravell

If the event is defined by your form (i.e. not a base class), then you
should have unrestricted access to the backing delegate; try just
setting "OnDetailChanged = null;".

If the event is defined in a base class, you will usually need to ask
the base class to do it via a protected method or similar.

But... note also that if the form is /completely/ closed, and passes
into the control of the GC, then everything will get cleaned up anyway;
event subscriptions are one-way; your form (when active) can keep the
targets (subscribers) alive, but the targets would not keep your form
alive (assuming standard setup).

Marc
 

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