« Home | Xwin32 MacroView Fonts » | ADSL2+ » | Task Bar Positioning » | Sysinternals to the rescue » | Cryptic IBM Filenames » | Regex Fail All » | Triplet » | csharpformat test » | ADO.NET Black Helicopters » | VSS 2005 Add Files »

Shut the ding up

 By default in Windows Forms a single line text box will sound an annoying ding/bell when the user presses the Enter key. Presumably this is suppose to inform the user that they shouldn’t need to press the Enter key as it is a single line text box, but its an annoying side effect if you want to perform some action when the enter key is pressed. An example in an application we are working on is a find textbox at the bottom of a tree control. Its convenient just to press enter once the text has been typed rather than moving the mouse over to another button and clicking. Note that these controls are on the main form so associating the enter key with closing a dialog is not suitable.

FindNextParty

A web search came up with the following solution (found in many locations). You consume the enter key so that the beep is not sounded. It seems like somewhat of a hack, so if anyone has a better approach please leave a comment.

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

 

Links to this post

Create a Link