0

I was wondering if there is a way to remove all functions added to a Button.Clicked().

For example I have this button:

Button btn = new Button();
btn.Clicked+=function_1;
btn.Clicked+=function_2;
btn.Clicked+=function_3;

void function_1(object sender,System.EventArgs e){
    //...
    //Same for function_2 and function_3
}

I need to remove all these functionalities from the button. I already know that I can do this:

btn.Clicked-=function_1;
btn.Clicked-=function_2;
btn.Clicked-=function_3;

But I am looking for a abstract way to do it. For example something like this (pseudocode):

foreach (f in btn.ClickedFuncions){
     btn.Clicked-=f;
}

Is it posible? How can I do it?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
David Zomada
  • 167
  • 1
  • 5
  • 22
  • Did you try btn.Clicked= null; ? – Serge Aug 30 '21 at 15:37
  • @Serge Did you try that? – Clemens Aug 30 '21 at 15:48
  • Yes, it didn't work. Button.Clicked only admits += or -= operators – David Zomada Aug 30 '21 at 15:50
  • no i didn't try this is why I am wondering. I know that events can be only added or removed, but I am not sure about null. Look at this link https://stackoverflow.com/questions/36084469/how-to-remove-all-eventhandler it has 24 upvotes. But as I uderstand it can work from inside only – Serge Aug 30 '21 at 15:51

0 Answers0