Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter key press in C#

Tags:

c#

.net

I tried this code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {     if (Convert.ToInt32(e.KeyChar) == 13)      {         MessageBox.Show(" Enter pressed ");     } } 

and this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {     if (e.KeyChar == Convert.ToChar(Keys.Enter))     {         MessageBox.Show(" Enter pressed ");     } } 

and this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {     if (e.KeyChar == Keys.Enter)     {         MessageBox.Show(" Enter pressed ");     } } 

but they're not working...

When I write something and press Enter, it doesn't work. It only highlights my text.

like image 299
Dave Doga Oz Avatar asked Sep 07 '12 12:09

Dave Doga Oz


People also ask

What is enter key in C?

The ASCII Code of ENTER KEY is 10 in Decimal or 0x0A in Hexadecimal.

What is enter key in programming?

Also called the "Return key," it is the keyboard key that is pressed to signal the computer to input the line of data or the command that has just been typed. It Was the Return Key.

How do you check if a key is pressed in C?

kbhit() is present in conio. h and used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file “conio.


2 Answers

Try this code,might work (Assuming windows form):

private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e) {     if (e.KeyChar == (char)13)     {         // Enter key pressed     } } 

Register the event like this :

this.textBox1.KeyPress += new  System.Windows.Forms.KeyPressEventHandler(CheckEnter); 
like image 96
Sandeep Pathak Avatar answered Oct 01 '22 00:10

Sandeep Pathak


You must try this in keydown event

here is the code for that :

private void textBox1_KeyDown(object sender, KeyEventArgs e)     {         if (e.Key == Key.Enter)         {             MessageBox.Show("Enter pressed");         }     } 

Update :

Also you can do this with keypress event.

Try This :

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)     {         if (e.KeyChar == Convert.ToChar(Keys.Return))         {             MessageBox.Show("Key pressed");         }     } 
like image 32
Ali Vojdanian Avatar answered Oct 01 '22 00:10

Ali Vojdanian