• 0

Clearing Events (removing all event handlers) [C#]


Question

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?

9 answers to this question

Recommended Posts

  • 0

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 */ };

  • 0

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.

:(

  • 0

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;

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That stupid annoying Sign in with Google on all these sites now... get the fk outta here
    • I was just being silly based on David Uzondu's comment ☺️
    • The unified inbox, when it arrives, will be a powerful argument for those who have > 1.
    • Fan Control V269 by Razvan Serea Fan Control is a powerful and versatile portable utility that allows you to monitor, control and customize the fans of your GPU and CPU to keep your machine cool and running smoothly. Fan Control supports a wide range of devices and hardware configurations, giving you complete control over your computer's cooling system. Fan Control backend is mainly based on LibreHardwareMonitor, an open source fork of the original OpenHardwareMonitor. This means that hardware compatiblity is entirely open for anyone to contribute, and doesn't rely on a single developer who may stop caring at some point. Combined with the plugin system, Fan Control is unlocked for many generations of hardware to come. Main features Guided setup process on first launch Save, edit and load multiple profiles Change the theme and color of the application. Multiple temperature sources ( CPU, GPU, motherboard, hard drives... ) Multiple fan curve functions, including a custom graph Mix fan curves or sensor togethers (max, min, average) Low resource usage Advanced tuning with steps, start %, stop %, response time and hysteresis FanControl V269 changelog: Allow only 1 service client at a time App title no longer show the full path Add service retry policy by default Fix pre-pairing issue between control and speed cards Fix a bug with ADLX % (duty) reporting Download: FanControl V269 | Installer ~20.0 MB (Open Source) View: Fan Control Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Collaborator
      Asgardi earned a badge
      Collaborator
    • Conversation Starter
      mobandz earned a badge
      Conversation Starter
    • Apprentice
      fernan99 went up a rank
      Apprentice
    • One Month Later
      nothanks earned a badge
      One Month Later
    • One Month Later
      B2Proxy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      470
    2. 2
      PsYcHoKiLLa
      243
    3. 3
      Skyfrog
      79
    4. 4
      FloatingFatMan
      73
    5. 5
      Michael Scrip
      60
  • Tell a friend

    Love Neowin? Tell a friend!