Search code examples
c#visual-studio-2008eventsdisconnect

How to disconnect an anonymous event?


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?


Solution

  • 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;