ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

RemoveClickEvent

Remove click event from given button.

Source

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

Example

btnTest.RemoveClickEvent();

Author: Dumitru Condrea

Submitted on: 25 nov. 2010

Language: C#

Type: System.Windows.Controls.Button

Views: 4819