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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s