Equivalent of PreTranslateMessage in C#

  • Thread starter Thread starter Vibhesh
  • Start date Start date
V

Vibhesh

Hello,

I have designed a form in C#. There are several controls
on the form. I want to trap all the events at the form
level before they are passed on to the controls.

For eg : Suppose there are two buttons on the form. At any
moment one of the two buttons will be focused. When I
press any key I want to perform some default processing at
the Form level before the events are caught by the Buttons.

I want something simillar to what PreTranslateMessage does
for CWnd in MFC.

Thanks.
 
Vibhesh,

You have two options. The first is to override the PreProcessMessage
method on the Form that is hosting the controls. The second is to implement
the IMessageFilter interface and register it with the static
AddMessageFilter method on the Application class. If you do the latter,
then the implementation will be called before the messages are dispatched to
the form (the applications message loop will pass to registered message
filters first).

Hope this helps.
 
Hello Nicholas,
Thanks for the reply.

Your first suggestion of overriding PreProcessMessage is
not working. Since at any moment the focus will be on any
one of the controls on the form, the key press event does
not passes to the PreProcessMessage or WinProc overriden
function of the Form.

Your second suggestion of IMessageFilter interface also
will not work in my solution.
I am calling the C# forms from my old C++ code and thus I
do not use Application class.

So do you have any other work-around for this problem.
Regards.



-----Original Message-----
Vibhesh,

You have two options. The first is to override the PreProcessMessage
method on the Form that is hosting the controls. The second is to implement
the IMessageFilter interface and register it with the static
AddMessageFilter method on the Application class. If you do the latter,
then the implementation will be called before the messages are dispatched to
the form (the applications message loop will pass to registered message
filters first).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

I have designed a form in C#. There are several controls
on the form. I want to trap all the events at the form
level before they are passed on to the controls.

For eg : Suppose there are two buttons on the form. At any
moment one of the two buttons will be focused. When I
press any key I want to perform some default processing at
the Form level before the events are caught by the Buttons.

I want something simillar to what PreTranslateMessage does
for CWnd in MFC.

Thanks.


.
 
Hello Vibhesh,

have you found soln for this?
Am too facing the same problem.

Please help me.

Thank you.

Regards,
Manoj,
itzmanojds @ gmail.com
 
Back
Top