Agregar
Control.Click+= NameEvent
Quitar
Control.Click+= NameEvent
sino te funciona usa esto
Funcion extendida, necesita estar en una clase estacica
static
public static void RemoveEvents(this Control b, List<String> eventos)
{
foreach (var item in eventos)
{
FieldInfo f1 = typeof(Control).GetField($"Event{item}",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Default);
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]);
}
}
Uso
TextBox textBox = new TextBox();
textBox.RemoveEvents(new List<String>() { "Validating", "KeyPress" });
Función Normal
public void RemoveEvents(Control b, List<String> eventos)
{
foreach (var item in eventos)
{
FieldInfo f1 = typeof(Control).GetField($"Event{item}",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Default);
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]);
}
}
Uso
TextBox textBox = new TextBox();
RemoveEvents(textBox,new List<String>() { "Validating", "KeyPress" });
Nota
La lista que revise cómo parametros son todos los eventos que queremos eliminar