Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Is there a way to find out if a control has any (or how many) event handler already defined?

I'm using Silverlight 4 and C#.

I want to create an event handler but only if the control has no event handlers already defined.
Just counting them would do it.

C#
LayoutRoot.MouseEnter += new MouseEventHandler(Volta_a_abrir);


Just need to know how many event handlers are defined on the LayoutRoot control.

Can it be done?

Thanks
Posted
Comments
[no name] 12-Jul-12 11:56am    
Look on the Events tab of the Properties window. I don't think that dynamic event handlers such as your example will show up there though.

Just press F12 on the control (in code) and you will see the underlying class (definition).
From this, you can figure out the number of events that are already defined.

Yuu can view the class hierarchy structure, and in Silverlight you will notice most classes are derived from FrameworkElement. As a result, you will see all of the events described in FrameworkElement in other classes as well.

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx[^] - this link describes the class hierarchy and events inside this control and the events that would be available in derives classes.

Similarly, you can look at ContentControl, ItemsControl etc, their events and their derived classes.

Further, a tool like ILDASM can help you look at the IL behind assemblies. This might help you get an idea on events that are being used in various assemblies and classes as well.
 
Share this answer
 
v2
Comments
Jorge J. Martins 12-Jul-12 12:29pm    
I think You missed the point.
I know witch events are to be activated and deactivated and I do it in code behind.
As an example I want to create an handler to the MouseEnter event on a control only if that event has no other handler already defined.
What I need to do in code behind e find out how many handlers are defined for that control and that event, if any.
Abhinav S 12-Jul-12 12:35pm    
Ah you mean events that may have already been attached.

AFAIK, you cannot do this from outside the owner of the event. You could have a AddHandler type of method. Control which events have already been handled in this method.

For e.g. private void Handled()
{
if(myButton.handled) return;
}
Espen Harlinn 13-Jul-12 4:09am    
5'ed!
Abhinav S 13-Jul-12 4:18am    
Thank you.
I guess you are looking for something like this:
public event EventHandler onevent;

private void func()
{
 EventHandler handlers = onevent;

 Delegate[] invocaltionList = handlers.GetInvocationList();

 if (invocaltionList.Length == 0)
 {
  onevent += OnEventHandler;
 }
}

public void OnEventHandler(object sender, EventArgs ea)
{
 
}


GetInvocationList returns the invocation list of the delegate where each element of array represents exactly one method.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Jorge J. Martins 12-Jul-12 12:56pm    
I'm accepting this answer for future reference.
For my solution, Abhinav's answer gave me a simpler idea.
Since I don't need to know witch handler is beeing handled I'm going for a local boolean turned true or false when a handler is attached or detached and an if(), using that boolean, for attaching another handler or not.

Thanks
Espen Harlinn 13-Jul-12 4:08am    
Brilliant :-D
Abhinav S 12-Jul-12 13:15pm    
My 5!
Espen Harlinn 13-Jul-12 4:09am    
Thank you, Abhinav!
Sergey Alexandrovich Kryukov 22-Jul-12 14:44pm    
Sure, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900