473,545 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capturing return keypress

68 New Member
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 ProcessDialogKe y() library to override it in the method, but cannot assign this to the ProcessDialogKe y 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 4566
mbewers1
68 New Member
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 New Member
Useful little bit of information...
May 19 '09 #3
developing
110 New Member
i actually have a similar question...thou ght 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 New Member
@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 New Member
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 New Member
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
10555
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 keydown and keypress events in Firefox, but then I can't seem to get the key code. I also don't know if the window.event.srcElement.type works with...
4
11980
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 problem in Firefox, not IE. I do know it's possible because Blogger.com's rich text editor does it, but their code is cryptic and separated into like 20...
7
20362
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 browsers? The script bellow is IE only. It fails FF 1.0 and NN 7. Also, if I can trap the keypress I would like to use it to tab to the next...
4
1535
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
1740
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 a function that I can put into the <text area> tag of each box, something like... <text area...
2
2270
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 where you can type "r" anywhere on the page while viewing a message and it will open a reply box, but if you type an "r" while entering text into a...
1
2960
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, System.Windows.Forms.KeyPressEventArgs e)
3
5242
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 handler does not see the combination. Now this control is contained within another control which is contained within another. The top most...
2
4756
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 one of the controls embedded in the tabControl has the focus. I'm doing this using the KeyDown event. However, when pressing the CTRL+C my computer...
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.