Having a TextBox control (DevExpress TextEdit to be precise) inside a WinForms form, I do not want the form to close when the user presses the enter key, if the focus is inside the text box.
I thought
filterTextBox.KeyDown +=
(sender, e) =>
e.Handled = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter;
would be sufficient, but it seems to be ignored and the form still closes.
My question is:
How to intentionally ignore the enter inside a single line text box control so that the form stays open?
Solution
The solution of Botz3000 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;
Update: Try handling the
PreviewKeyDownevent, too. The MSDN documentation explains it pretty well in the Remarks section. By settingIsInputKeyto true, you can override the default behavior so that your TextBox can handle the key. You’ll need to do this inPreviewKeyDownand then handle the key as you already do inKeyDown.EDIT: Not working: Previously suggested
EnterMoveNextControlproperty