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?
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.
This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the javascript category.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With