BrunoLM
BrunoLM

Reputation: 100331

Check if the control has events on Click EventHandler

I want to know if a control has a method assigned on the Click event.

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

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

I need something that can at least tell me if the control has or not some method on the click event.

Upvotes: 6

Views: 12070

Answers (5)

Mattia72
Mattia72

Reputation: 1169

I've checked the topic suggested by Norman H (Determine list of event handlers bound to event)

It worked for me in case of "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();
    }

Possible values for eventName are in the output of this GetFields command:

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

So the possible values:

...
[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}

Upvotes: 7

Dr Herbie
Dr Herbie

Reputation: 3940

You can only do that from within the class that owns the event handler (Button in this case). Within the Button class you could test for Click != null to see if there were subscribers. Unfortunately, since I suspect the Button is the framework class, you're out of luck.

Upvotes: 0

PerlDev
PerlDev

Reputation: 437

If you need test if the delegate exists/subscribed, you can try -= first, in catch run +=

Upvotes: 1

Norman H
Norman H

Reputation: 2262

You might do some reading into this topic Determine list of event handlers bound to event , it appears that it may be somewhat related to what you are trying to do.

Upvotes: 3

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

You can't do that. Event is just add/remove methods to add delegate to the chain. Only the class that has an event can know if there are subscribers to the event.

Even more, you should not need to know if there are subscribers to the event of a control. Why do you need this information?

Upvotes: 2

Related Questions