How to remove all event handlers from an event

后端 未结 18 1652
再見小時候
再見小時候 2020-11-22 01:20

To create a new event handler on a control you can do this

c.Click += new EventHandler(mainFormButton_Click);

or this

c.Cli         


        
18条回答
  •  旧时难觅i
    2020-11-22 01:46

    removes all handlers for button: save.RemoveEvents();

    public static class EventExtension
    {
        public static void RemoveEvents(this T target) where T : Control
        {
           var propInfo = typeof(T).GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
            var list = (EventHandlerList)propInfo.GetValue(target, null);
            list.Dispose();
        }
    }
    

提交回复
热议问题