Hi everyone,

I am making a windows application in which I want that when user presses enter key while textBox1 has focus then an event is generated. I tried using following but it doesn't workout:-

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

Please help.

Recommended Answers

All 2 Replies

Hi
Do this:

if (e.KeyChar == (char)Keys.Enter)
            {
                //do something
            }

inside textBox1_KeyPress function

Hi
Do this:
Help with Code Tags
csharp Syntax (Toggle Plain Text)

1. if (e.KeyChar == (char)Keys.Enter)
2. {
3. //do something
4. }

inside textBox1_KeyPress function

Thanks for replying, I got the result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.