博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
remove all event handlers from a control
阅读量:2238 次
发布时间:2019-05-09

本文共 2141 字,大约阅读时间需要 7 分钟。

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
delegates = new List
(); 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(); }

 

转载于:https://www.cnblogs.com/zeroone/p/3632239.html

你可能感兴趣的文章
RNN与机器翻译
查看>>
用 Recursive Neural Networks 得到分析树
查看>>
RNN的高级应用
查看>>
TensorFlow-7-TensorBoard Embedding可视化
查看>>
轻松看懂机器学习十大常用算法
查看>>
一个框架解决几乎所有机器学习问题
查看>>
特征工程怎么做
查看>>
机器学习算法应用中常用技巧-1
查看>>
决策树的python实现
查看>>
了解 Sklearn 的数据集
查看>>
如何选择优化器 optimizer
查看>>
一文了解强化学习
查看>>
CART 分类与回归树
查看>>
seq2seq 的 keras 实现
查看>>
seq2seq 入门
查看>>
什么是 Dropout
查看>>
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>
按时间轴简述九大卷积神经网络
查看>>