Enter key press in C#

前端 未结 14 714
臣服心动
臣服心动 2020-12-01 06:06

I tried this code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Convert.ToInt32(e.KeyChar) == 13) 
    {
        MessageBox.S         


        
相关标签:
14条回答
  • 2020-12-01 06:23
    private void textBoxKontant_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Return)
                {
                    MessageBox.Show("Enter pressed");
                }
            }
    

    Screenshot:

    0 讨论(0)
  • 2020-12-01 06:24
    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.

    0 讨论(0)
  • 2020-12-01 06:24

    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

    0 讨论(0)
  • 2020-12-01 06:28

    Instead of using Key_press event you may use Key_down event. You can find this as below
    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 ");
            }
         }
    
    0 讨论(0)
  • 2020-12-01 06:29

    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);
    
    0 讨论(0)
  • 2020-12-01 06:30
    private void TextBox1_KeyUp(object sender, KeyEventArgs e)
    {
         if (e.KeyCode == Keys.Enter)
         {
             MessageBox.Show("Enter pressed");
         }
     }
    

    Worked for me.

    0 讨论(0)
提交回复
热议问题