Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 695
Ashkan Mobayen Khiabani Avatar asked Dec 21 '25 04:12

Ashkan Mobayen Khiabani


1 Answers

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.

like image 178
Simon Karlsson Avatar answered Dec 23 '25 19:12

Simon Karlsson