Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell when the enter key is pressed in a TextBox?

Tags:

c#

winforms

enter

Basically, I want to be able to trigger an event when the ENTER key is pressed. I tried this already:

private void input_KeyDown(object sender, KeyEventArgs e)     {         if (e.Equals("{ENTER}"))         {             MessageBox.Show("Pressed enter.");         }     } 

But the MessageBox never shows up. How can I do this?

like image 923
Jon Avatar asked Aug 04 '12 05:08

Jon


People also ask

How do you check if the Enter key is pressed?

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.

How do you call a function when Enter is pressed?

You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.

Which of the following event is triggered whenever the user presses Enter key while editing any input field in the form?

The keyup event occurs when a keyboard key is released.


2 Answers

Give this a shot...

private void input_KeyDown(object sender, KeyEventArgs e)  {                             if(e.KeyData == Keys.Enter)        {           MessageBox.Show("Pressed enter.");       }              } 
like image 185
Chris Gessler Avatar answered Oct 17 '22 19:10

Chris Gessler


To add to @Willy David Jr answer: you also can use actual Key codes.

private void input_KeyDown(object sender, KeyEventArgs e) {     if (e.KeyChar == 13)     {         MessageBox.Show("Pressed enter.");     } } 
like image 40
Mantas Daškevičius Avatar answered Oct 17 '22 19:10

Mantas Daškevičius