Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annoying beep when I hit enter in a textbox

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!

like image 396
Nathan Avatar asked Dec 10 '13 22:12

Nathan


1 Answers

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:

  • KeyDown
  • KeyPress
  • KeyUp

At the point you're setting e.SupressKeyPress, the KeyPress event has already occurred.

like image 54
Reed Copsey Avatar answered Nov 14 '22 14:11

Reed Copsey