The sample code below will remove all Click events from button1

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); button1.Click += button1_Click;
button1.Click += button1_Click2;
button2.Click += button2_Click;
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
} private void button1_Click2(object sender, EventArgs e)
{
MessageBox.Show("World");
} private void button2_Click(object sender, EventArgs e)
{
RemoveClickEvent(button1);
} 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]);
}
}
}

or

void OnFormClosing(object sender, FormClosingEventArgs e)
{
foreach(Delegate d in FindClicked.GetInvocationList())
{
FindClicked -= (FindClickedHandler)d;
}
}

or

    Directly no, in large part because you cannot simply set the event to null.

    Indirectly, you could make the actual event private and create a property around it that tracks all of the delegates being added/subtracted to it.

    Take the following:

    List<EventHandler> delegates = new List<EventHandler>();

    private event EventHandler MyRealEvent;

    public event EventHandler MyEvent
{
add
{
MyRealEvent += value;
delegates.Add(value);
} remove
{
MyRealEvent -= value;
delegates.Remove(value);
}
} public void RemoveAllEvents()
{
foreach(EventHandler eh in delegates)
{
MyRealEvent -= eh;
}
delegates.Clear();
}

remove all event handlers from a control的更多相关文章

  1. 事件处理(Event Handlers) ng-click操作

    事件处理(Event Handlers) ng-click操作 step 10 本文主要通过介绍ng-click方法来对angularjs中的事件处理方法做个了解. 1.切换目录 git checko ...

  2. clone Control event handlers at run time

    var btn2 =newButton(); btn2.Text= btn1.Text; btn2.size = btn1.size; To clone all events of any WinFo ...

  3. Implementing the On Item Checked Event for the TListView Control

    The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...

  4. Liferay7 BPM门户开发之4: Activiti事件处理和监听Event handlers

    事件机制从Activiti 5.15开始引入,这非常棒,他可以让你实现委托. 可以通过配置添加事件监听器,也可以通过Runtime API加入注册事件. 所有的事件参数子类型都来自org.activi ...

  5. [React] Pass Data To Event Handlers with Partial Function Application

    In this lesson we’ll see how to pass an item’s id value in an event handler and get the state to ref ...

  6. AngularJS学习--- 事件处理(Event Handlers) ng-click操作 step 10

    本文主要通过介绍ng-click方法来对angularjs中的事件处理方法做个了解. 1.切换目录 git checkout step- npm start 2.效果 点击右边的小图片,那么左边框中将 ...

  7. [Angular Tutorial] 12 -Event Handlers

    在这一步中,您将会在电话细节页面添加一个可点击的电话图片转换器. ·电话细节页面展示了当前电话的一张大图片和几张相对较小的略图.如果我们能仅仅通过点击略图就能把大图片换成略图就好了.让我们看看用Ang ...

  8. Writing a Reusable Custom Control in WPF

    In my previous post, I have already defined how you can inherit from an existing control and define ...

  9. win10 UWP 等级控件Building a UWP Rating Control using XAML and the Composition API | XAML Brewer, by Diederik Krols

    原文:Building a UWP Rating Control using XAML and the Composition API | XAML Brewer, by Diederik Krols ...

随机推荐

  1. Linux---文件类型及权限操作

    文件类型: 用ls命令查看目录下所属文件时,每行的第一个字母标识着文件对应的文件类型 '-':代表普通文件 'd':代表目录 'c':字符设备文件 'b':块设备文件 's':套接字文件 'l':符号 ...

  2. object-c 1

    多个参数的写法 (方法的数据类型)函数名:(参数1数据类型)参数1的数值的名字 参数2的名字: (参数2数据类型) 参数2值的名字 …. ; 举个例子,一个方法的定义: -(void) setKids ...

  3. 【python】Windows安装Beautiful Soup

    环境:win10,python 3.5,Beautiful Soup 4.1   步骤1:设定python为系统环境变量,具体设置如下图,在文本后加上";C:\Python35;C:\Pyt ...

  4. Redhat 6 配置CentOS yum source

    由于最近曝出linux的bash漏洞,想更新下bash,于是 想到了配置CentOS yum source. 测试bash漏洞的命令: env x='() { :;}; echo "Your ...

  5. <a>作Form表单提</a>

    1. Form表单<%using (Html.BeginForm("AddRoles", "RoleManage", FormMethod.Post, n ...

  6. MSSql ID自动增长删除数据重1开始

    dbcc checkident('db_Tome1.dbo.员工信息表',reseed,0) 注:dbcc checkident('表名',reseed,0)

  7. 转载:STM32之中断与事件---中断与事件的区别

    这张图是一条外部中断线或外部事件线的示意图,图中信号线上划有一条斜线,旁边标志19字样的注释,表示这样的线路共有19套.图中的蓝色虚线箭头,标出了外部中断信号的传输路径,首先外部信号从编号1的芯片管脚 ...

  8. SDC(4)–set_clock_groups 与–add选项

    1,set_clock_groups  -exclusive 有多个时钟,但是多个时钟不会同时生效 例如: 2,-add 只有一个时钟输入源,但是始终的频率等可能变 例如:

  9. SQL 返回数量一定的行

    1. 限制返回的行 select top 10 * from tablename 2. 返回随机n行 select top n * from tablename order by newid()

  10. windows store app Lifecycle

    1.Activated 2.Suspended 3.Resumed 4.Terminated 对应的 js代码: (function () { "use strict"; WinJ ...