Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 290109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:58:04+00:00 2026-05-12T05:58:04+00:00

Problem: I have a document class which contains a list of objects. These objects

  • 0

Problem: I have a document class which contains a list of objects. These objects raise events such as SolutionExpired, DisplayExpired etc. The document needs to respond to this.

Documents can sometimes exchange objects, but a single object should never be ‘part’ of more than one document.

My document class contains a bunch of methods which serve as event handlers. Whenever an object enters the document, I use AddHandler to set up the events, and whenever an object is removed from the document I use RemoveHandler to undo the damage. However, there are cases where it’s difficult to make sure all the steps are properly taken and I might thus end up with rogue event handlers.

Long story short; how do I remove all the handlers that are pointing to a specific method? Note, I don’t have a list of potential event sources, these could be stored anywhere.

Something like:

RemoveHandler *.SolutionExpired, AddressOf DefObj_SolutionExpired
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:58:04+00:00Added an answer on May 12, 2026 at 5:58 am

    You can use Delegate.RemoveAll(). (The part you’re interested in is in button2_Click)

    public void Form_Load(object sender, EventArgs e) 
    { 
        button1.Click += new EventHandler(button1_Click);
        button1.Click += new EventHandler(button1_Click);
        button2.Click += new EventHandler(button2_Click);
        TestEvent += new EventHandler(Form_TestEvent);
    }
    event EventHandler TestEvent;
    void OnTestEvent(EventArgs e)
    {
        if (TestEvent != null)
            TestEvent(this, e);
    }
    void Form_TestEvent(object sender, EventArgs e)
    {
        MessageBox.Show("TestEvent fired");
    }
    void button2_Click(object sender, EventArgs e)
    {
        Delegate d = TestEvent as Delegate;
        TestEvent = Delegate.RemoveAll(d, d) as EventHandler;
    }
    void button1_Click(object sender, EventArgs e)
    {
        OnTestEvent(EventArgs.Empty);
    }
    

    You should note that it doesn’t alter the contents of the delegates you pass in to it, it returns an altered delegate. Consequently, you won’t be able to alter the events on a button you’ve dropped on a form from the form, as button1.Click can only have += or -= used on it, not =. This won’t compile:

    button1.Click = Delegate.RemoveAll(d, d) as EventHandler;
    

    Also, be sure that wherever you’re implementing this you’re watching out for the potential of race conditions. You could end up with some really strange behavior if you’re removing handlers from an event that is being called by another thread!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 210k
  • Answers 210k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Adding JSF tag in renderer which is invoked in Renderer… May 12, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer Use the bracket notation member operator: var value = obj[0]['@attr'];… May 12, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer I suggest using Nokogiri instead. It should give you the… May 12, 2026 at 9:53 pm

Related Questions

I'm trying to validate a multiline textbox control as follows: My content page (I'm
I'm trying to make a little tag list doohickey for adding and removing tags.
Okay so I have a Window in WPF. I add the following line inside
I have been having some problems with this for a few days now... I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.