• 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

    • Feels like Hitman in Bond skin. But overall a great game! 👍
    • Microsoft released new Defender update for Windows 11, 10, Server ISO installations by Sayan Sen Microsoft releases new Windows Defender update packages very frequently to protect against various newly discovered malware. Once a while every three months or so, the company also pushes out these updates to Windows images (WIM and VHD) and ISOs, that are used to install Windows. Hence with the newest Windows 11 update available via the official MCT tool, you should get these definitions. This update package is necessary as a Windows installation image may contain old, outdated anti-malware definitions and software binaries. Aside from better security, these updates can also provide improved performance benefits in some cases. When a new Windows installation is set up, there may be a temporary security risk due to outdated Microsoft Defender protection in the OS installation images. This happens because the antimalware software included in these images might not be up to date. Thus Microsoft says that these updated definitions essentially help close this protection gap. Microsoft delivered the latest security definitions for Windows images via security intelligence update version 1.445.323.0. The Defender package version is also the same. It applies to Windows 11, Windows 10 ESU, Windows 10 Enterprise LTSC 2021, Win 10 Ent LTSC 2019, Win 10 Ent LTSB 2016, Windows Server 2022, Windows Server 2019, and Windows Server 2016. Microsoft writes: "This package updates the anti-malware client, anti-malware engine, and signature versions in the OS installation images to following versions: Platform version: 4.18.26040.7 Engine version: 1.1.26040.8 Security intelligence version: 1.447.236.0" From Microsoft's security bulletin, we learn that the security intelligence update version 1.447.236.0 was released early last month and adds threat detections for various malware like trojan, backdoor exploits, ransomware, stealers, AutoKMS, and more. For those wondering, the latest intelligence update is version 1.451.297.0 at the time of writing.
    • Hello, Hope all is well. I am in UK.  
  • Recent Achievements

    • Dedicated
      Mark Spruce earned a badge
      Dedicated
    • Collaborator
      conkir earned a badge
      Collaborator
    • Rising Star
      olavinto went up a rank
      Rising Star
    • One Month Later
      lamborghiniv10 earned a badge
      One Month Later
    • Week One Done
      lamborghiniv10 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      504
    2. 2
      PsYcHoKiLLa
      271
    3. 3
      Skyfrog
      74
    4. 4
      +Edouard
      73
    5. 5
      Steven P.
      71
  • Tell a friend

    Love Neowin? Tell a friend!