Ianmac45 Posted October 14, 2006 Share Posted October 14, 2006 so, let's say i've got an anonymous method handling an event Button b = new Button(); b.Click += delegate { /* some code */ }; is there any way to completely clear the event handler list for any particular event WITHOUT direct references to specific handlers? Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/ Share on other sites More sharing options...
0 smurfiness Posted October 16, 2006 Share Posted October 16, 2006 You should be able to just set the event to null. b.Click = null; Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587958858 Share on other sites More sharing options...
0 Linkinfamous Posted October 16, 2006 Share Posted October 16, 2006 dannysmurf said: You should be able to just set the event to null. b.Click = null; If memory serves me, doing that should only work from inside the class itself. Solution: Make a new button class. class MyButton : Button { public void ClearEvents() { this.Click = null; } } MyButton b = new MyButton(); b.Click += delegate { /* some code */ }; Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587959470 Share on other sites More sharing options...
0 Ianmac45 Posted October 17, 2006 Author Share Posted October 17, 2006 nope, just checked that doesn't work either :( Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587963479 Share on other sites More sharing options...
0 lexecutil Posted October 17, 2006 Share Posted October 17, 2006 Well this is really against OO principles.. It's not really up to you to stop other classes from recieving a subscribed event, though I'm sure it is possible. I will see if I can find a solution for you now. :) Dan Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587963487 Share on other sites More sharing options...
0 lexecutil Posted October 17, 2006 Share Posted October 17, 2006 OK I'm pretty sure this isn't possible sorry. :( The only other possiblilty if you really want todo this is through reflection, but really there is almost definatly a better way to design your app so this isn't required.. :) Dan Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587963626 Share on other sites More sharing options...
0 Ianmac45 Posted October 18, 2006 Author Share Posted October 18, 2006 well, i grudgingly worked around it by having a reference to each and every single event delegate. and then i'd remove and add them one at a time, setting the reference then the handler on the object. it really sucks, but it works i guess i should look into redesigning it. :( Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587964371 Share on other sites More sharing options...
0 John Veteran Posted October 18, 2006 Veteran Share Posted October 18, 2006 Just curious... Why would you want to do this? :blink: Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587964585 Share on other sites More sharing options...
0 mattbta Posted October 18, 2006 Share Posted October 18, 2006 dannyres said: Well this is really against OO principles.. It's not really up to you to stop other classes from recieving a subscribed event, though I'm sure it is possible. I will see if I can find a solution for you now. :) Dan True...but you need to ensure that classes subscribing to said event unsubscribe before they are destroyed. b.Click -= delegate; Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587965800 Share on other sites More sharing options...
0 Ianmac45 Posted October 18, 2006 Author Share Posted October 18, 2006 John said: Just curious... Why would you want to do this? :blink: i've got a tabbed application for my company where all the tabs have a standard context menu. i chose to have a static class with a single context menu for all open tabs. the tabs would then ask the class to update the shared context menu and make sure all actions in the context menu get routed to the right tab. so, for each menu item, i simply wanted to be able to reset the entire event instead of having to reference the last added event handler. ... yea Link to comment https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/#findComment-587966393 Share on other sites More sharing options...
Question
Ianmac45
so, let's say i've got an anonymous method handling an event
is there any way to completely clear the event handler list for any particular event WITHOUT direct references to specific handlers?
Link to comment
https://www.neowin.net/forum/topic/503337-clearing-events-removing-all-event-handlers-c/Share on other sites
9 answers to this question
Recommended Posts