Windows Develop Bookmark and Share   
 index > Windows Forms General > trace all events of a control
 

trace all events of a control

can I have an event handler called by a winforms control whenever any of the control's events are signaled?

I would like to fill a ListBox with the names of all the events fired by a control for debugging purposes.

thanks,
SteveRichter  Friday, September 18, 2009 3:35 PM

Wrong question...I think.
I willask and answer what I guess is the 'right' question in a minute.

Let's answer the question you asked.
Sure just use the default signature and subscribe to every event.
Make all events point to this handler.

void Handles_AnyEvent(object sender, EventArgs e)
{
// add a TimeStamp to your Listbox.
// check the parameter objects to find out what happened.
return;
}

I think you want reallyto know how many subscribers there are to any and all events associated with a control.
For this you would need to check the Invocation List of each event delegate defined for the control's class.
But, you are severely restricted in how you can reference an event outside of the defining class.
All that you can do is subscribe and unsubscribe to the events. Any other type of reference is prohibited.
The innards of eventmembers are treated as if they were private.

Events are supposed to be anonymous. The Publisher doesn't care who subscribes to the event.
The Subscriber doesn't care who else subscribes to the event.
You do not even have control over the order in which handlers are called!

You can access the private members of a class through Reflection.
Once you have a collectioin of all events, then you could interrogate their Invocation Lists to see if there are any subscribers.

Hope this helps.

Rudedog =8^D


Mark the best replies as answers. "Fooling computers since 1971."
  • Marked As Answer bySteveRichter Monday, September 21, 2009 2:20 PM
  •  
Rudedog2  Friday, September 18, 2009 10:55 PM

Wrong question...I think.
I willask and answer what I guess is the 'right' question in a minute.

Let's answer the question you asked.
Sure just use the default signature and subscribe to every event.
Make all events point to this handler.

void Handles_AnyEvent(object sender, EventArgs e)
{
// add a TimeStamp to your Listbox.
// check the parameter objects to find out what happened.
return;
}

I think you want reallyto know how many subscribers there are to any and all events associated with a control.
For this you would need to check the Invocation List of each event delegate defined for the control's class.
But, you are severely restricted in how you can reference an event outside of the defining class.
All that you can do is subscribe and unsubscribe to the events. Any other type of reference is prohibited.
The innards of eventmembers are treated as if they were private.

Events are supposed to be anonymous. The Publisher doesn't care who subscribes to the event.
The Subscriber doesn't care who else subscribes to the event.
You do not even have control over the order in which handlers are called!

You can access the private members of a class through Reflection.
Once you have a collectioin of all events, then you could interrogate their Invocation Lists to see if there are any subscribers.

Hope this helps.

Rudedog =8^D


Mark the best replies as answers. "Fooling computers since 1971."
  • Marked As Answer bySteveRichter Monday, September 21, 2009 2:20 PM
  •  
Rudedog2  Friday, September 18, 2009 10:55 PM
Only indirectly. Controls almost exclusively respond to Windows messages. Override the WndProc() method and use Console.WriteLine(m.ToString()) to dump the message. Or add it to a list. With generous helpings of Charles Petzold, you'd know exactly what is going on. Spy++ gives you the exact same info without having to write code or derive your own control class.

Hans Passant.
nobugz  Saturday, September 19, 2009 12:47 AM

Hi SteveRichter,

>can I have an event handler called by a winforms control whenever any of the control's events are signaled?

Based on my understanding, you want to detect any fired events of a control. Could you please tell me why you want to detect all these events? May be we can find a better solution for this requirement.

Normally in an application, we just need to handle some events of a control. Attaching to all events could affect the performance. Besides, not all events share the same type of handler.

For example, button’s Click event is EventHandler type, while Button’s MouseClick event is MouseEventHandler type. You cannot register it to the same method.

As for nobugz’s suggestion, if you capture the Win32 message, you can hardly recognize which event is fired. And thanks for Rudedog and nobugz's contributions.

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 21, 2009 7:14 AM

Hi SteveRichter,

>can I have an event handler called by a winforms control whenever any of the control's events are signaled?

Based on my understanding, you want to detect any fired events of a control. Could you please tell me why you want to detect all these events? May be we can find a better solution for this requirement.



sure. I would like to change the caret in a RichTextBox. From another post in the MSDN forum I understand I have to CreateCaret and ShowCaret after certain events in the RichTextBox like GotFocus and MouseDown. You do this because the RTB control is always changing the caret back to the default caret. This works for me except for when I resize the form by dragging the border. I added my "recreate the caret" code to the Resize event, but this had no effect. I am looking for a quick way to see all the events fired by RichTextBox when the containing form is resized by dragging the border.

thanks for all the help.
SteveRichter  Monday, September 21, 2009 2:29 PM
Hmmm...

The beauty, power and the realintent of events is that there are designed to be anonymous, which means loose coupling between classes.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, September 21, 2009 11:04 PM

You can use google to search for other answers

Custom Search

More Threads

• How do I get a static background image in a ListView
• Using .NET Framework 2.0
• Where did Datagridview.AllowUserToAddRows go? (Amongst others)...
• change format of NumericUpDown - C#
• Help in getting all the controls in a form
• Back color of status bar
• Difference between arraylist and hashtable
• redirection of forms in wimdows application
• How to open applicatipns (like MyComputer or any programs)inside VB.net windows form
• BUG: LinkLabel