How would it be possible to remove all event handlers of the 'Click' event of a 'Button'?

后端 未结 6 612
孤街浪徒
孤街浪徒 2020-11-28 11:56

I have a button control, and I\'d need to remove all the event handlers attached to its Click event.

How would that be possible?

Button button = GetB         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 12:35

    I found this answer here on StackOverflow:

    How to remove all event handlers from a control

    private void RemoveClickEvent(Button b)
    {
        FieldInfo f1 = typeof(Control).GetField("EventClick", 
            BindingFlags.Static | BindingFlags.NonPublic);
        object obj = f1.GetValue(b);
        PropertyInfo pi = b.GetType().GetProperty("Events",  
            BindingFlags.NonPublic | BindingFlags.Instance);
        EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
        list.RemoveHandler(obj, list[obj]);
    }
    

    Which the origional poster found here:

提交回复
热议问题