15

I have a command button on a winform. So, if I have something like:

myButton.Click += MyHandler1;
myButton.Click += MyHandler2;
myButton.Click += MyHandler3;

How can I tell if any particular MyHandler has already been added to the Click event so it doesn't get added again somewhere else in my code?

I've read how you can use GetInvocationList() for your own event's information. But I get errors when trying to get the items for my command button using various combinations. It says,

"The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=."

What am I missing?

[Edit] - I'd like to accentuate this question that Ahmad pointed out. It's a kludge and should be easier IMHO, but it looks like it might just work.

Community
  • 1
  • 1
IAmAN00B
  • 1,913
  • 6
  • 27
  • 38
  • 3
    You can't. The best thing you can do is a `myButton.Click -= MyHandler1` before you add it. – slugster Nov 04 '10 at 02:40
  • 1
    possible duplicate of [Determine list of event handlers bound to event](http://stackoverflow.com/questions/660480/determine-list-of-event-handlers-bound-to-event) – Ahmad Mageed Nov 04 '10 at 02:42
  • @Ahmad - Good eye. I surely wouldn't have seen that one! However, for clarity, I believe my question is still valid. – IAmAN00B Nov 04 '10 at 10:01

2 Answers2

13

If you're in doubt if your handler is already added then just remove it and add it again. If your handler wasn't added in the first place, your removal is just ignored.

myButton.Click -= MyHandler1;
myButton.Click += MyHandler1;

You could also create one method for attaching to an event, and make sure that the code is only run once.

private bool handlersAdded;
private void AddHandlers()
{
    if (this.handlersAdded) return;
    myButton.Click += MyHandler1;
    this.handlersAdded = true;
}
Paw Baltzersen
  • 2,662
  • 3
  • 24
  • 33
  • @sotn Why not? If you have a static event, let's call it "StaticClick", your can still remove and add you handler from it: `MyStaticClass.StaticClick-=MyHandler1;`. It's just a method pointer you remove and/or add from/to a list. Anyways, I'd never use static events, if you find yourself in need of such a thing then rethink your design. – Paw Baltzersen Jan 12 '17 at 17:57
  • It might not be "your" design but a thirdparty dll you use.. Think of this example: You want to attach an event handler to your pc's sound driver and stream some data to several IPs. That event handler will be static because you would not want to create multiple listeners for each ip but create a single event listener and broadcast it from there (because of performance reasons). What you suggest is remove that handler and reattach it for every single IP.. Risky. I hope you understand my point. – yakya Jan 13 '17 at 08:19
3

The use of GetIvocationList can only be done from within the owner of the event (myButton in your case), that's one of the ideas behind events (as opposed to delegates).

Like Slugster said, you can't check the invocation list from outside myButton, but you can try and remove MyHandler# before adding it.

Neowizard
  • 2,981
  • 1
  • 21
  • 39