ianinspain
asked on
500pts: Generic unsubscribing an event?
Hi there,
I wonder if anyone can help, i need to unsubscribe from an event but i don't know the method at runtime.. Probably better if i explain with an example..
myGraphics.Click += new EventHandler(myGraphics_Cl ick); // this is set in a form of some kind..
I then pass around myGraphics to difference forms so i need to reset the .Click event to myGraphics_Click but the problem is that it is still set to myGraphics_click on the original form before it was passed so hence the event fires on both forms :-)
Is there a way i can NULL the click event or something, or iterate through all subscribers to the click event so i can decide how to unsubscribe..
The problem is that i will probably have to unsubscribe the original form when i have arrived in my new form...
Confused? I am :-) .... Any ideas how to do this... really apprecaite any help
thanks in advance
I wonder if anyone can help, i need to unsubscribe from an event but i don't know the method at runtime.. Probably better if i explain with an example..
myGraphics.Click += new EventHandler(myGraphics_Cl
I then pass around myGraphics to difference forms so i need to reset the .Click event to myGraphics_Click but the problem is that it is still set to myGraphics_click on the original form before it was passed so hence the event fires on both forms :-)
Is there a way i can NULL the click event or something, or iterate through all subscribers to the click event so i can decide how to unsubscribe..
The problem is that i will probably have to unsubscribe the original form when i have arrived in my new form...
Confused? I am :-) .... Any ideas how to do this... really apprecaite any help
thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks greg! i sort of have it working...
I had to make the event a static event because when the ovject with the click evetn arrives in another form I don't have a reference to it ... so i technically can't do myGraphics.Clicked -= ClickEventHandler; or it would try to unsubscribe in the current form whcih i haven't subscribed to yet... I presume i am correct on this?
Is there anyway i can check the object to see if anything has subscribed.... as i was getting a double event somewhere in my code.... so if i could do
if myGraphics.clicked == OriginalForm.ClickEventHan dler
{
// hass been subscribed in original form
// then unsubscribe....
// then subscribe in this form
}
else
{
// jsut subscribe in this form..
}
I had to make the event a static event because when the ovject with the click evetn arrives in another form I don't have a reference to it ... so i technically can't do myGraphics.Clicked -= ClickEventHandler; or it would try to unsubscribe in the current form whcih i haven't subscribed to yet... I presume i am correct on this?
Is there anyway i can check the object to see if anything has subscribed.... as i was getting a double event somewhere in my code.... so if i could do
if myGraphics.clicked == OriginalForm.ClickEventHan
{
// hass been subscribed in original form
// then unsubscribe....
// then subscribe in this form
}
else
{
// jsut subscribe in this form..
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.. no it isn't the class... but thanks everyone for the input.... i am going to split the points...
http://www.thescarms.com/dotNet/eventhandler.aspx