【问题标题】:Exploring/Iterating UI Event's探索/迭代 UI 事件
【发布时间】:2011-05-30 13:30:40
【问题描述】:

在 C# 应用程序中,我们可以添加 事件处理程序,例如 this.button3.Click += new System.EventHandler(this.button3_Click);,我们也可以删除 this.button3.Click -= new System.EventHandler(this.button3_Click);,这意味着我们可以在 UI 控件中添加任意数量的事件,所以我的问题是我如何从 UI 中探索或迭代和删除特定事件。 例如。如果我们能以某种方式在 ListBox 上填充控件的所有事件(如果可能的话)并删除 Selected One 。

PS:我有点像初级程序员,所以我认为代码反射处理这部分。 最好的

【问题讨论】:

标签: c# visual-studio-2010 events uicontrol


【解决方案1】:

您可以使用如下代码轻松显示组件所有可能事件的列表

EventInfo[] allEvents = Button1.GetType().GetEvents(System.Reflection.BindingFlags.Instance
    | System.Reflection.BindingFlags.Public);
foreach (EventInfo curEvent in allEvents)
{
    EventsListBox.Items.Add(curEvent.Name);
}

但是,似乎不可能知道哪些方法分配给了代表,在组件外部发生事件时将调用哪些方法。可以参考How do I find out if a particular delegate has already been assigned to an event?Determine list of event handlers bound to eventIterating through event handlers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多