Home > Net >  How to stop key Down of the form
How to stop key Down of the form

Time:09-26

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.

CodePudding user response:

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;
}
  •  Tags:  
  • c#
  • Related