The code below detects if the “Enter” key is pressed. It’s useful to know the key pressed by the user so you can do some action, such as set the focus on the next textbox if the enter key is hit.

So, let create a new visual studio windows form project using C#, and then add a Textbox control to the main form.

Add the code below to the key_up event of the textbox. The KeyValue ‘13’ represents the Enter key.

1.	private void textBox1_KeyUp (object sender, KeyEventArgs e)
2.	       {
3.	           if (e.KeyValue == 13)
4.	               MessageBox.Show("Enter key pressed!!");
5.	       }

 

Run the solution, set focus to the textbox, then press the enter key. The output should be as below.

Output

Related Articles

 

 

Last modified: May 5, 2018

Comments

Write a Reply or Comment

Your email address will not be published.