473,405 Members | 2,404 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.

Capturing return keypress

I've never handled keysroke events before and I would like to implement a Return keystroke event in a Windows form to bring up a list of items in a datagrid as well as habing a search button in the form.

With the help of a tutorial, I have coded the following event:

Expand|Select|Wrap|Line Numbers
  1. private void ReturnResults(object o, KeyPressEventArgs e)
  2.         {
  3.             char c = e.KeyChar;
  4.             if (c == (char) Keys.Return)
  5.             {
  6.                 SearchAdmin();
  7.             }
  8.             else
  9.             {
  10.                 e.Handled = false;
  11.             }
  12.  
  13.             base.ProcessDialogKey(Keys.Return);
  14.         }
  15.  
and have assigned the event handler in the constructor of my Form, with the following line:

Expand|Select|Wrap|Line Numbers
  1. this.KeyPress += new KeyPressEventHandler(ReturnResults);
  2. //this.ProcessDialogKey += new KeyPressEventHandler(ReturnResults);
  3.  
I have also been told that the KeyPress event does not handle control keys, so I am using the ProcessDialogKey() library to override it in the method, but cannot assign this to the ProcessDialogKey method group (commented out part).

Unfortunately, nothing happens when I press return with the code as it stands. And, when I first tried implementing it, the form would EXIT when return was pressed, but I don't know how that would have happened?

Am I missing something? Please advise.

Matt
May 19 '09 #1
6 4559
In case anyone else has this problem:

You need to put the following in the constructor of the form:
Expand|Select|Wrap|Line Numbers
  1. this.KeyPreview = true;
  2.  
if you want to handle a key being pressed when controls are active:
The control will then work on the form every time!
May 19 '09 #2
IanWright
179 100+
Useful little bit of information...
May 19 '09 #3
developing
110 100+
i actually have a similar question...thought id ask it in this thread rather than starting a new one

i have a infinite for loop that keeps on going. i want to make it so that if 'escape' key is pressed at anytime, the loop stops. whats the best way to do this?

im new to multithreading, delegates, and events but im thinking of having a key press event that is fired whenever the escape key is pressed and it stops the loop. i understand the theory but not sure how the code would look.

thuoghts? sample code? links? thanks.
May 20 '09 #4
IanWright
179 100+
@developing
Depends how you'd like to do it and how you're code it setup. You could have

Expand|Select|Wrap|Line Numbers
  1. while(Console.ReadKey().Key != Key.Escape)
  2. {
  3.    // Do stuff
  4. }
  5.  
or something more like:

Expand|Select|Wrap|Line Numbers
  1. private bool cancel;
  2.  
  3. private void Setup()
  4. {
  5.    this.KeyDown += new KeyDownEventHandler(ToggleCancel);
  6.  
  7.    // or using lambdas
  8.    this.KeyDown += new KeyDownEventHandler((sender, e) => 
  9.    {
  10.     if(e.Key == Keys.Escape)
  11.       {
  12.          this.cancel = true;
  13.       }
  14.    });
  15.  
  16.    Process();
  17. }
  18.  
  19. private void Process()
  20. {
  21.    cancel = false;
  22.    while(!cancel)
  23.    {
  24.       // Do stuff
  25.    }
  26. }
  27.  
  28. private void ToggleCancel(Object sender, KeyEventArgs e)
  29. {
  30.    if(e.Key == Keys.Escape)
  31.    {
  32.       this.cancel = true;
  33.    }
  34. }
  35.  
May 21 '09 #5
developing
110 100+
thanks for the skeleton, im trying the second part.

lets see if this doesn't help me out. thanks
May 21 '09 #6
developing
110 100+
i got it working. thanks...had to put the stuff inside the while(!cancel) loop and the event on different threads to get it workin
May 22 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: coolsti | last post by:
Can someone help me enhance this code snippet which works only for IE so that it will also work for Firefox? I have been trying all sorts of things and have gotten to where I can capture the...
4
by: Jay Xx | last post by:
I have an IFrame in design mode. I've tried a bunch of things to capture key presses in that IFrame, but I can't seem to get it. I can capture key presses outside the IFrame fine. I have this...
7
by: jerrygarciuh | last post by:
Hello, I have been playing with various Googled solutions for capturing the <Enter> key to suppress form submission. My first question is whether anyone has a script that works in all common...
4
by: alien2_51 | last post by:
I have a form with several buttons, I'd like to default to a specific button on the Enter keypress event, How would I do this...?
14
by: @sh | last post by:
Has anyone a function handy that I can apply to various textboxes within a form, each textbox will permit most characters but I want to ban certain characters from each textbox. Therefore I need...
2
by: jbigham | last post by:
Hello, I'd like to capture key events using javascript, but don't want to process such events when the user is typing into an input box or into a textarea. As an example, gmail has a feature...
1
by: sULs | last post by:
Hi all Hoping someone can help !! I am trying to catch F2 - 6 key presses in a winforms application Code as follows private void frmMain_KeyPress(object sender,...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
2
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
I have a tabcontrol where I want to capture the CTRL+C key combination. My tabcontrol has on its tabpages some treeView controls. My intention is to intercept the event of pressing the CTRL+C when...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.