Check if “Enter” key is pressed using C#

Tutorials PanelPosted by

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

Check for Even or Odd Number Using C#

Get the IP Address of a local machine Using C#

Create SQL server database backup in ASP.NET using C#

Introduction to Dictionary Initializer in C#

 

 

Leave a Reply