I'm trying to have some stuff happen when a user presses enter in a textbox, and it works, but it makes a very annoying DING windows error sound when I do. I've looked up my problem, and apparently adding e.SuppressKeyPress = true; before the stuff, and e.Handled = true; after the stuff, but my program still makes the sound. This is the code I'm using:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
if (e.KeyCode == Keys.Enter)
{
// A bunch of stuff goes here that I want to
// happen when the user hits enter
}
e.Handled = true;
}
Am I doing something wrong? This is what the other people said you have to do, but it's not working for me for some reason...
Thanks!
You need to handle KeyDown, not KeyUp, in order to suppress the KeyPress event. The KeyDown documentation explains why, as it lists the order of events as:
At the point you're setting e.SupressKeyPress, the KeyPress event has already occurred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With