473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problem with OnKeyPress override...

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!
Nov 16 '05 #1
4 1979
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

Nov 16 '05 #2
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

Nov 16 '05 #3
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

In article <p0**************@cpmsftngxa10.phx.gbl>, you say...
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


Nov 16 '05 #4
Mohamed,

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

Colin

In article <NA**************@cpmsftngxa10.phx.gbl>,
mo************@egdsc.microsoft.com says...
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.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
0
by: Paul Rees | last post by:
I am wondering if anyone has encountered a similar problem to the one that I am having. I have an ASP.NET page which has a table and a dropdownlist that was added at design time. The majority of...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
2
by: paolo | last post by:
Thanks in advance to everyone that will give me help for these questions. The function formatCurrency is used to auto-format a numeric value with thousands separators and a decimal separator. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.