How to remove all event handlers from a control
2011-11-06 12:45
471 查看
public partial class Form1:Form { publicForm1() { InitializeComponent(); button1.Click+= button1_Click; button1.Click+= button1_Click2; button2.Click+= button2_Click; } privatevoid button1_Click(object sender,EventArgs e) { MessageBox.Show("Hello"); } privatevoid button1_Click2(object sender,EventArgs e) { MessageBox.Show("World"); } privatevoid button2_Click(object sender,EventArgs e) { RemoveClickEvent(button1); } privatevoidRemoveClickEvent(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]); } } }
相关文章推荐
- remove all event handlers from a control
- How To Build Multi-control Components when Inheriting from an Existing Control (Intro and TextBox Example)
- 解决:The virtual machine was unable to remove all stack frame runnning old code from the call stack.问题
- How to remove k__BackingField from Json data
- How to remove Oracle Database from your computer---By Roger Gong
- How to add/remove applications from ‘Open With’ window
- ZZ: How to remove 'Open in Windows Explorer' from the 'Actions Menu'
- How to remove Oracle Database from your computer---By Roger Gong
- How to pass ViewState value from web form page to web user control page?
- How to remove k__BackingField from Json data
- SharePoint 2007 (MOSS/WSS) - how to remove "Download a Copy" context menu from a Document Library
- Send event to Host (Windows Form) from a WPF Control -Part 1
- Shell / sed Program To Remove All C and C++ Comments From Program File
- How to remove Oracle Database from your computer---By Roger Gong
- How to permanently remove TFS Source Control association bindings?
- How to manually remove an infected file from your computer
- How to remove a block from the particular page?
- how to reference the parent form from the WPF control(Control in ElementHost)
- Git: How to remove ignored files from your repository's directory
- Send Custom Event to Host (Windows Form) from a WPF Control -Part 2