gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:28:58 25 4
gpt4 key购买 nike

我想知道某个控件是否具有分配给 Click 事件的方法。

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

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

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

最佳答案

我已经查看了 Norman H 建议的主题( Determine 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}

关于c# - 检查控件是否在 Click EventHandler 上有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3212721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com