Winforms .net: textbox on enter key press causes a default beep

Question:

I  have a standard .net winforms textbox on a form.

when the enter key is pressed and the textbox has focus, a default beep sound is fired…. this is really annoying, and I need to get rid of it, because I am using this textbox as a search box.

Please help

To reproduce: create a new win form application, open form 1 in design view, drag a textbox from the toolbox onto the form. run the application, click in the textbox, hit the return (enter) key.

 

Answer

You can add a KeyPress event handler with this code:

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r')
        {
            e.Handled = true;
        }
    }

This seems to only work with KeyPress and not KeyDown and/or KeyUp

If you want to go with the button approach, you can also make the button invisible

Advertisements