Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Removing event handlers

Removing event handlers

Scheduled Pinned Locked Moved C#
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    archies_san
    wrote on last edited by
    #1

    Is there any way to remove all the event handlers associated with buttons click event without using -= operator

    D M B 3 Replies Last reply
    0
    • A archies_san

      Is there any way to remove all the event handlers associated with buttons click event without using -= operator

      D Offline
      D Offline
      Diana Fernandez
      wrote on last edited by
      #2

      With an event you can use only += or -= operator. Actually keyword "event" is modifying a delegate for this behaviour.

      1 Reply Last reply
      0
      • A archies_san

        Is there any way to remove all the event handlers associated with buttons click event without using -= operator

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, A suggestion I gave some time ago and got no feedback, and never tried it myselfe! But maybe it helps you. The control class has a member called Events, which is a list of EventHandler (System.ComponentModel.EventHandlerList). This class provides a method called "RemoveHandler". Please let me know if it works for you! All the best, Martin

        A 1 Reply Last reply
        0
        • A archies_san

          Is there any way to remove all the event handlers associated with buttons click event without using -= operator

          B Offline
          B Offline
          Bekjong
          wrote on last edited by
          #4

          No there isn't. You can't set an event to be null or something for example, which is strange because it is initialized to be null. Here's my way of going around it:

          public event EventHandler ExampleEvent;

              public void UnHook()
              {
                  Collection<EventHandler> ExampleHandlers = new Collection<EventHandler>();
          
                  foreach (Delegate d in this.ExampleEvent.GetInvocationList())
                      ExampleHandlers.Add(d as EventHandler);
          
                  foreach (EventHandler e in ExampleHandlers)
                      this.ExampleEvent-= e;
              }
          

          I don't know if there's a better way for this, but this works for me...

          Standards are great! Everybody should have one!

          1 Reply Last reply
          0
          • M Martin 0

            Hello, A suggestion I gave some time ago and got no feedback, and never tried it myselfe! But maybe it helps you. The control class has a member called Events, which is a list of EventHandler (System.ComponentModel.EventHandlerList). This class provides a method called "RemoveHandler". Please let me know if it works for you! All the best, Martin

            A Offline
            A Offline
            archies_san
            wrote on last edited by
            #5

            Hi Martin, Thanks for the reply. Control class has a "protected" property called Events which I can't use in my code(which is a Outlook addin)becoz I am using CommandBarButton.

            M 1 Reply Last reply
            0
            • A archies_san

              Hi Martin, Thanks for the reply. Control class has a "protected" property called Events which I can't use in my code(which is a Outlook addin)becoz I am using CommandBarButton.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, Don't care! I now tested it with an inherited Control, and found out, that it would not fit your needs. All the best, Martin

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups