Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Created December 11, 2019 17:39
Show Gist options
  • Save amitkhare/a4365eba6db696c8e4c4db57b0ee3ebb to your computer and use it in GitHub Desktop.
Save amitkhare/a4365eba6db696c8e4c4db57b0ee3ebb to your computer and use it in GitHub Desktop.
C# Remove all handlers from any event
private void RemoveEvent(object item, string eventStr = "DoubleClick")
{
FieldInfo f1 = typeof(Control).GetField("Event"+ eventStr,
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(item);
PropertyInfo pi = item.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(item, null);
list.RemoveHandler(obj, list[obj]);
}
@amitkhare
Copy link
Author

USAGE

RemoveEvent(this, "DoubleClick");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment