Search code examples
c#winformsenter

How do I tell when the enter key is pressed in a TextBox?


Basically, I want to be able to trigger an event when the ENTER key is pressed. I tried this already:

private void input_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Equals("{ENTER}"))
        {
            MessageBox.Show("Pressed enter.");
        }
    }

But the MessageBox never shows up. How can I do this?


Solution

  • Give this a shot...

    private void input_KeyDown(object sender, KeyEventArgs e) 
    {                        
        if(e.KeyData == Keys.Enter)   
        {  
            MessageBox.Show("Pressed enter.");  
        }             
    }