Problem with OnKeyPress override...

C

Colin

Hello all,

I am working on a project using Windows Forms, and I ran into a bit of a
snag... I need to be able to use keystrokes as shortcuts in the program,
and I currently accomplish it using the following:

-----[ Begin code snip ]-----
protected override void OnKeyPress (KeyPressEventArgs e) {

switch (e.KeyChar) {
case 'b':
e.Handled = true;
mnuNavigationBack.PerformClick();
break;
case 'f':
e.Handled = true;
mnuNavigationForward.PerformClick();
break;
default:
break;
}

base.OnKeyPress(e);
}
-----[ End code snip ]-----

Everything works great until the user opens another window (such as say
an About dialog or a MessageBox)... Once the other dialog is opened,
closed, and the focus is redirected back to the main form, the
keypresses are no longer registered with the main form...

Does anyone know why this would happen?

I searched through the list archives via groups.google.com, and couldnt
find anything referring to anything close to this...

Any advice would be much appreciated.

Thanks!
 
M

Mohamoss

Hi Colin
I have tried to simulate your situation but the problem with not
reproduced. You might be doing something that prevents this normal
behavior. Are using any API call that get the windows handle of your form
for when I tried that the event didn't fire. Would you send more sniffs
of your code
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
M

Mohamoss

Hi Colin
One thing I forgot to mention is that this event (key pressed) is not
supposed to be handled by the form but by the controls on the form. If the
form has any controls it will captured by these controls.
If you want it to he handled by the form you need to change the KeyPreview
Property from false to True.
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
C

Colin

Mohamed,

Thank you for trying to look reproduce the problem I am seeing. It is
appreciated!

I'm not using any direct API calls... Just the normal built in C#
Windows Forms classes...

I will send more of the pertinent code in a bit.

Thanks again,
Colin
 
C

Colin

Mohamed,

I do indeed have they KeyPreview property set to true. Thanks for
mentioning it though.

Colin
 

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