gmajor
gmajor

Reputation: 11

KeyEvent doesn't detect any keys C#

I'm currently stumped. I can't seem to get the KeyEvent to work. Simple code like this just won't respond to the key I'm pressing. I've tried KeyDown and KeyPress. No errors while compiling... what is causing this?? It will just let me enter the E key without prompting the MEssage box.

 private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.E)
        {
            MessageBox.Show("E");
        }
    }

Upvotes: 1

Views: 711

Answers (2)

star
star

Reputation: 164

You should change the Form KeyPreview property to true where textbox1 is located.

Upvotes: 1

Paul
Paul

Reputation: 4249

I think you should be using the PreviewKeyDown Event, for example, instead of the standard key events, as sometimes these events are blocked an not bubbled up through the control.

Upvotes: 3

Related Questions