[WinForm]移除控件某个事件

2020-12-13 05:10

阅读:622

标签:winform   style   color   使用   os   width   

关键代码:

        /// 
        /// 移除控件某个事件
        /// 
        /// 控件
        /// 需要移除的控件名称eg:EventClick
        public static void RemoveControlEvent(this Control control, string eventName)
        {
            FieldInfo _fl = typeof(Control).GetField(eventName, BindingFlags.Static | BindingFlags.NonPublic);
            if (_fl != null)
            {
                object _obj = _fl.GetValue(control);
                PropertyInfo _pi = control.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
                EventHandlerList _eventlist = (EventHandlerList)_pi.GetValue(control, null);
                if (_obj != null && _eventlist != null)
                    _eventlist.RemoveHandler(_obj, _eventlist[_obj]);
            }
        }

使用代码:

        private void WinRemoveControlEventTest_Load(object sender, EventArgs e)
        {
            button1.RemoveControlEvent("EventClick");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1_Click");
        }

[WinForm]移除控件某个事件,搜素材,soscw.com

[WinForm]移除控件某个事件

标签:winform   style   color   使用   os   width   

原文地址:http://www.cnblogs.com/Yan-Zhiwei/p/3858418.html


评论


亲,登录后才可以留言!