The Button Click event can only be the left-hand-side of a += or -=, so querying it directly may not be possible. You could create a MulticastDelegate object, add your button click event handers to that and then use GetInvokationList() to list the delegates to add to the click event (at initialization time) and to query for the delegates currently attached to Click. You could also use the list to remove the delegates from click.
Proposed As Answer byxalnixSunday, June 08, 2008 1:28 PM
You can't enumerate the subscribers of an event from outside of the class contained in the event. The event keyword causes two methods to be created: add and remove for the event--nothing else. There's no way to get at the multicast delegate from outside the class. From inside the class you can use the GetInvocationList method on the event delegate (if there is one).http://www.peterRitchie.com/blog
Marked As Answer byjack 321Wednesday, June 11, 2008 5:50 AM
Peter, aren't all "events" delegates (or multicastdelegates, to be precise)? One blogger has categorize the C# "event" and"delegate" keywords as "sugar coating" for the MulticastDelegate class. From experience, the Button control 's CLICK event is restricted so that you can't get at the underlying multicast. I suspect all MS standard controls are restricted this way. In fact, I suspect all events are restricted outside of the class in which they are defined. But, if you implement your own events, then your class has access to the multicast via a type cast...
MulticastDelegate m = (MulticastDelegate)myEvent;
//Then you can get the delegate list...
Delegate [] dlist = m.GetInvocationList();
//and then iterate through to find and invoke the ones you want...
foreach(Delegate d in dlist)
{
if(aListOfObjects.Contains(d.Target))
{
object [] p = { /*put your parameters here*/ };
d.DynamicInvoke(p);
}
else
MessageBox.Show("Not Invoking the Event for " + d.Target.ToString() + ":" + d.Target.GetHashCode().ToString());
}
Marked As Answer byjack 321Wednesday, June 11, 2008 5:51 AM
Edited byxalnixFriday, September 12, 2008 10:05 PMfix typo
TRID, Here's a great thread that might help you get there. Since the problem is to get at "button1" click event, and since it can't be on the right hand side (from outside of the class where defined), the solution is complex. But reflection might work as discussed and answered in topic "How to find registered event handlers?" in this forum, posted a few minutes ago.
Peter, aren't all "events" delegates (or multicastdelegates, to be precise)? One blogger has categorize the C# "event" and"delegate" keywords as "sugar coating" for the MulticastDelegate class. From experience, the Button control 's CLICK event is restricted so that you can't get at the underlying multicast. I suspect all MS standard controls are restricted this way. In fact, I suspect all events are restricted outside of the class in which the are defined. But, if you implement your own events, then your class has access to the multicast via a type cast...
MulticastDelegate m = (MulticastDelegate)myEvent;
//Then you can get the delegate list...
Delegate [] dlist = m.GetInvocationList();
//and then iterate through to find and invoke the ones you want...
foreach(Delegate d in dlist)
{
if(aListOfObjects.Contains(d.Target))
{
object [] p = { /*put your parameters here*/ };
d.DynamicInvoke(p);
}
else
MessageBox.Show("Not Invoking the Event for " + d.Target.ToString() + ":" + d.Target.GetHashCode().ToString());
}
Not all events are implemented as a delegate member. The event keyword defaults to implementing events via a single multi-cast delegate, but you can override that. For example:
private EventHandlerList events = new EventHandlerList();
In this case, for another class to iterate all the methods subscribed to myevent, it would have to have access to the events member. Another possibility is to override the event add/remove and only use non-multi-cast delegates (i.e. there would only be one subscriber at a time).http://www.peterRitchie.com/blog
Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.