Filtering focus messages/events

P

paulo

Hello,

I'm developing a Windows Forms application with .NET 2.0 and I need to
centrally get all focus messages / events. For example, when a user tabs
from one TextBox to another in the application, I want to know that
TextoBox1 lost focus and TextBox2 got focus. I tried to add a
IMessageFilter to the application, but i don't seem to get anything
relevant, other than keyup and keydowns.
Is there anyway to do this?
Thanks in advance for any help given.

Regards,
paulo
 
J

Jeffrey Tan[MSFT]

Hi paulo,

Thanks for your post!

IMessageFilter is not a reliable way to catch all the messages in the
application. Actually, IMessageFilter is preprocessing before the normal
translate/dispatch cycle of the message pump, and it will only see posted
messages. The messages that pass to the Windows procedure through
SendMessage method will not be placed in the message pump, so the
IMessageFilter will not see them.

If you only want to catch all the messages to the main Form, you may
override Form class's WndProc method. Normally all the messages to Form
class will first pass this method first. However, in your scenario, the
TextBoxes themselves are separate windows, they have their own WndProc, so
the focus messages of TextBoxes will not be passed to Form's WndProc, while
are passed to TextBoxes' WndProc.

The only reliable way to catch all the messages in a Winform application is
Windows Message Hook. You can install a WH_GETMESSAGE to the main GUI
thread of your application, and then you will get all the messages in your
main GUI thread. Because this is a Local Windows Hook, there is no need to
write the hook procedure in a dll, you can just use p/invoke in C# to get
this done. Please refer to the KB below:
"How to set a Windows hook in Visual C# .NET"
http://support.microsoft.com/kb/318804/en-us

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

paulo

Hello Jeffrey!

Thank you so much for your reply.
I will definitely look into Windows hooks.

Kind regards,
paulo
 
J

Jeffrey Tan[MSFT]

Ok, if you need further help, please feel free to post. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top