Summit
Summit

Reputation: 2266

How to stop key Down of the form

I have a Form1_KeyDown event for form1, but while I am typing some text in the Textbox on the Form, the event gets triggered.

How can I stop that event when I am typing inside the textboxes on the form.

Upvotes: 0

Views: 58

Answers (1)

Avo Nappo
Avo Nappo

Reputation: 630

There are two ways:

  1. Set form's KeyPreview to false. Which is the default, so you must have explicitly changed it, presumably for a reason. Otherwise the controls of on the form always get the keyboard events first.

  2. Add an active control check in Form1_KeyDown, like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (this.ActiveControl == textBox1) return;
    var k = e.KeyCode;
}

Upvotes: 2

Related Questions