Apr 24
Typically when you press the Enter key while typing in a TextBox control, you will hear the computer beep.
To prevent this beep, handle the Enter key in the KeyPress event, and set the Handled property to true. For example:
void TextBox_KeyPress( object sender, KeyPressEventArgs e ) { switch (e.KeyChar) { case 'r': // perform necessary action e.Handled = true; break; } }
You can also do that:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
}
Great answer!!
e.SuppressKeyPress = true;
works perfectly.
Amaziiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiinngggggg :):):)
In winforms, if your form has a button asigned to the acceptButton property, then e.Handled = true works good.
But if your form doesn’t, use the e.SuppressKeyPress = true
This happens because the e.Handled pass the event to the underlying layer (which is the form) and since there is no acceptButton set, then th Beep Sound activates trying to tell you there is kind of an error not knowing what to do with the event.
Hope it helps