private void idTextEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
e.Handled = true;
SearchButtonClick(sender, EventArgs.Empty);
}
}
I have a text box where this code check fires for every single keypress except for the Enter/Return key which for some reason does nothing. The cursor that is active on the textbox disappears, so I’m thinking it changes focus before the keydown event can fire but I’m not sure. How can I get the return key to stop deselecting the box and register as a keypress. Also there is no other code that would set the enter key to have a different functionality and it’s a pretty simple one text box screen for testing.
If you have an
AcceptButtonset then you’ll get the behaviour you are seeing.I tried your code with my sample form and it worked as expected.
I then set the
AcceptButtonto one of the buttons and the text box stopped responding to theEnter. SettingAcceptsReturnon the text box had no effect.