[c#] Enter key press in C#

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.

This question is related to c# .net

The answer is


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");
        }
    }

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

Worked for me.


You could use this code:

abc_KeyDown(abc, new KeyEventArgs(Keys.Enter));

Try this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
        switch (e.Key.ToString())
        {
                case "Return":
                        MessageBox.Show(" Enter pressed ");
                        break;
         }
}

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");
    }
}

Result


Instead of using Key_press event you may use Key_down event. You can find this as belowEvent
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 ");
        }
     }

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.


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
   }


}

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

This worked for me.


private void textBoxKontant_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                MessageBox.Show("Enter pressed");
            }
        }

Screenshot:


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


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");
        }
    }

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.