Enter key press in C#
C#.NetC# Problem Overview
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.
C# Solutions
Solution 1 - C#
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);
Solution 2 - C#
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");
}
}
Solution 3 - C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter Key Pressed");
}
}
This allows you to choose the specific Key you want, without finding the char value of the key.
Solution 4 - C#
private void textBoxKontant_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
MessageBox.Show("Enter pressed");
}
}
Screenshot:
Solution 5 - C#
KeyChar is looking for an integer value, so it needs to be converted as follows:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToInt16(Keys.Enter))
{
MessageBox.Show("Testing KeyPress");
}
}
Solution 6 - C#
Instead of using Key_press event you may use Key_down event. You can find this as below[![Event][1]][1]
[1]: https://i.stack.imgur.com/55DA4.jpg
after double clicking here it will automatically this code
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
}
Problem solved now use as you want.
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(" Enter pressed ");
}
}
Solution 7 - C#
private void TextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter pressed");
}
}
Worked for me.
Solution 8 - C#
private void Input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MessageBox.Show("Enter pressed");
}
}
This worked for me.
Solution 9 - C#
Also you can do this with keypress event.
private void textBox1_EnterKeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some code what you wanna do
}
}
Solution 10 - C#
For me, this was the best solution:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
}
Solution 11 - C#
Keeping in mind that
Keys.Return is different to Keys.Enter
//Makes it easier to Type user and password and press Enter,
//Rather than using the mouse to Click the Button all the time
private void Txt_Password_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Return)
{
Btn_Login_Click(null, null);
}
}
Hope this help someone.
Solution 12 - C#
private void Ctrl_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
Select all required textboxes; type in new Events name Ctrl_KeyUp.
See YouTube by BTNT TV: https://www.bing.com/videos/searchq=c%23+how+to+move+to+next+textbox+after+enterkey&view=detail&mid=999E8CAFC15140E867C0999E8CAFC15140E867C0&FORM=VIRE
Solution 13 - C#
You could use this code:
abc_KeyDown(abc, new KeyEventArgs(Keys.Enter));
Solution 14 - C#
Try this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.Key.ToString())
{
case "Return":
MessageBox.Show(" Enter pressed ");
break;
}
}