Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disconnect an anonymous event? [duplicate]

Possible Duplicate:
How do I Unregister 'anonymous' event handler

I have code like this:

        Binding bndTitle = this.DataBindings.Add("Text", obj, "Title");
        bndTitle.Format += (sender, e) =>
        {
            e.Value = "asdf" + e.Value;
        };

How do I now disconnect the Format event?

like image 572
AngryHacker Avatar asked Sep 20 '10 22:09

AngryHacker


People also ask

How do I get rid of anonymous event handler?

Strictly speaking you can't remove an anonymous event listener unless you store a reference to the function. Since the goal of using an anonymous function is presumably not to create a new variable, you could instead store the reference in the element itself: element. addEventListener('click',element.

Can you remove event listener with anonymous function?

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the javascript category.

Why do we use anonymous event handlers in Windows Forms?

Anonymous methods are a simplified way for you to assign handlers to events. They take less effort than delegates and are closer to the event they are associated with. You have the choice of either declaring the anonymous method with no parameters or you can declare the parameters if you need them.

How addEventListener works?

The method addEventListener() works by adding a function, or an object that implements EventListener , to the list of event listeners for the specified event type on the EventTarget on which it's called.


1 Answers

You can't do that, unfortunately. You could create a local to hold the lambda if you remove the event in the same scope:

Binding bndTitle = this.DataBindings.Add("Text", obj, "Title");
EventHandler handler = (sender, e) =>
{
    e.Value = "asdf" + e.Value;
};

bndTitle.Format += handler;
// ...
bndTitle.Format -= handler;
like image 81
Chris Schmich Avatar answered Oct 09 '22 08:10

Chris Schmich