检查控件是否在Click EventHandler上有事件

时间:2010-07-09 12:53:25

标签: c# .net event-handling

我想知道控件是否在Click事件上分配了方法。

Button b = new Button();
b.Click += (sender, e) => { };

/* What I want */
b.Click.Count // 1

我需要的东西至少可以告诉我控件是否有点击事件的某种方法。

5 个答案:

答案 0 :(得分:5)

我已经检查了Norman HDetermine list of event handlers bound to event)建议的主题

如果" EventClick "

,它对我有用
    hasClickEventHandler = HasEventHandler(buttonControl, "EventClick");
    Assert.AreEqual(hasClickEventHandler, true);


    private bool HasEventHandler(Control control, string eventName)
    {
        EventHandlerList events =
            (EventHandlerList)
            typeof(Component)
             .GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance)
             .GetValue(control, null);

        object key = typeof(Control)
            .GetField(eventName, BindingFlags.NonPublic | BindingFlags.Static)
            .GetValue(null);

        Delegate handlers = events[key];

        return handlers != null && handlers.GetInvocationList().Any();
    }

eventName 的可能值位于此 GetFields 命令的输出中:

typeof(Control).GetFields(BindingFlags.NonPublic | BindingFlags.Static)

所以可能的值:

...
[6]: {System.Object EventAutoSizeChanged}
[7]: {System.Object EventKeyDown}
[8]: {System.Object EventKeyPress}
[9]: {System.Object EventKeyUp}
[10]: {System.Object EventMouseDown}
[11]: {System.Object EventMouseEnter}
[12]: {System.Object EventMouseLeave}
[13]: {System.Object EventMouseHover}
[14]: {System.Object EventMouseMove}
[15]: {System.Object EventMouseUp}
[16]: {System.Object EventMouseWheel}
[17]: {System.Object EventClick}
[18]: {System.Object EventClientSize}
[19]: {System.Object EventDoubleClick}
[20]: {System.Object EventMouseClick}
[21]: {System.Object EventMouseDoubleClick}
[22]: {System.Object EventMouseCaptureChanged}
[23]: {System.Object EventMove}
[24]: {System.Object EventResize}
[25]: {System.Object EventLayout}
[26]: {System.Object EventGotFocus}
[27]: {System.Object EventLostFocus}
[28]: {System.Object EventEnabledChanged}
[29]: {System.Object EventEnter}
[30]: {System.Object EventLeave}
[31]: {System.Object EventHandleCreated}
[32]: {System.Object EventHandleDestroyed}
[33]: {System.Object EventVisibleChanged}
[34]: {System.Object EventControlAdded}
[35]: {System.Object EventControlRemoved}
[36]: {System.Object EventChangeUICues}
[37]: {System.Object EventSystemColorsChanged}
[38]: {System.Object EventValidating}
[39]: {System.Object EventValidated}
[40]: {System.Object EventStyleChanged}
[41]: {System.Object EventImeModeChanged}
[42]: {System.Object EventHelpRequested}
[43]: {System.Object EventPaint}
[44]: {System.Object EventInvalidated}
[45]: {System.Object EventQueryContinueDrag}
[46]: {System.Object EventGiveFeedback}
[47]: {System.Object EventDragEnter}
[48]: {System.Object EventDragLeave}
[49]: {System.Object EventDragOver}
[50]: {System.Object EventDragDrop}
[51]: {System.Object EventQueryAccessibilityHelp}
[52]: {System.Object EventBackgroundImage}
[53]: {System.Object EventBackgroundImageLayout}
[54]: {System.Object EventBindingContext}
[55]: {System.Object EventBackColor}
[56]: {System.Object EventParent}
[57]: {System.Object EventVisible}
[58]: {System.Object EventText}
[59]: {System.Object EventTabStop}
[60]: {System.Object EventTabIndex}
[61]: {System.Object EventSize}
[62]: {System.Object EventRightToLeft}
[63]: {System.Object EventLocation}
[64]: {System.Object EventForeColor}
[65]: {System.Object EventFont}
[66]: {System.Object EventEnabled}
[67]: {System.Object EventDock}
[68]: {System.Object EventCursor}
[69]: {System.Object EventContextMenu}
[70]: {System.Object EventContextMenuStrip}
[71]: {System.Object EventCausesValidation}
[72]: {System.Object EventRegionChanged}
[73]: {System.Object EventMarginChanged}
[74]: {System.Object EventPaddingChanged}
[75]: {System.Object EventPreviewKeyDown}

答案 1 :(得分:3)

您可能会对此主题Determine list of event handlers bound to event进行一些阅读,看起来它可能与您尝试的内容有些相关。

答案 2 :(得分:1)

你做不到。事件只是添加/删除方法以将委托添加到链中。只有具有事件的类才能知道该事件是否有订阅者。

更重要的是,你不应该知道是否有控件事件的订阅者。你为什么需要这些信息?

答案 3 :(得分:0)

如果您需要测试委托是否存在/订阅,您可以尝试 - =首先,在catch run + =

答案 4 :(得分:0)

您只能在拥有事件处理程序的类中执行此操作(在本例中为Button)。 在Button课程中,您可以测试Click != null以查看是否有订阅者。不幸的是,因为我怀疑Button是框架类,所以你运气不好。