none
how to remove a eventhandler RRS feed

  • Question

  • for example,
     this.button2.Click += new System.EventHandler(this.button2_Click);
    (another 1-second-Timer is running)


    I want the button click is available in 1 second and not available in another second.
    So , the primary trouble is I don't know how to remove /readd eventhandler .

    Sunday, October 19, 2008 5:37 AM

Answers

  • My post is wrong, the new shouldn't be there.  Drop the "new System.EventHandler" from both adding and removing the handler.
    • Marked as answer by gem_pplus Sunday, October 19, 2008 4:34 PM
    Sunday, October 19, 2008 4:02 PM
  •  this.button2.Click -= new System.EventHandler(this.button2_Click);
    Sunday, October 19, 2008 8:17 AM

All replies

  •  this.button2.Click -= new System.EventHandler(this.button2_Click);
    Sunday, October 19, 2008 8:17 AM
  • how to analyze new ? i am confusing with "new" objects. 
    Sunday, October 19, 2008 3:11 PM
  • My post is wrong, the new shouldn't be there.  Drop the "new System.EventHandler" from both adding and removing the handler.
    • Marked as answer by gem_pplus Sunday, October 19, 2008 4:34 PM
    Sunday, October 19, 2008 4:02 PM
  • It wasn't wrong, using new is quite appropriate here.
    Hans Passant.
    Monday, October 20, 2008 12:57 PM
    Moderator
  • Remove a new EventHandler?
    Monday, October 20, 2008 1:01 PM
  • A delegate is a type.  Adding or removing an event handler requires an instance of that type.  Which requires the "new" keyword.  C# allows dropping the "new" keyword but that's just syntactic sugar, it still generates "new" in the IL.  Under the hood, the MulticastDelegate uses the delegate instance's Method and Target properties to find the delegate instance that needs to be removed.
    Hans Passant.
    Monday, October 20, 2008 3:28 PM
    Moderator
  • As usual, you are correct.
    Monday, October 20, 2008 3:57 PM