简体   繁体   中英

Remove a dynamically added event handler

I have a code like:

button1.Click += (s, e) =>
{

};

Now how is it possible to remove this handler dynamically? something like:

button1.Click = null;

The point with events is that they are subscribe/unsubscribe, it is not the intention that you should unsubscribe other events then your own. Therefore you need to keep track of your event:

var click = (s, e) =>
{

};

button1.Click += click;

You can then unsubscribe it by:

button1.Click -= click;

EDIT

Seems you can use the approach suggested here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM