Having a TextBox
control (more precisely, DevExpress TextEdit
) inside a WinForms form, I donβt want to close the form when the user presses the enter key if the focus is inside the text field.
I thought
filterTextBox.KeyDown += (sender, e) => e.Handled = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter;
would be enough, but that seems to be ignored, and the form is still closing.
My question is:
How to deliberately ignore input inside a single-line text field control so that the form remains open?
Decision
Botz3000 solution worked for me:
filterTextBox.PreviewKeyDown += (sender, e) => e.IsInputKey = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter; filterTextBox.KeyDown += (sender, e) => e.Handled = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter;
source share