12

I want to disable the beep sound that I get when I press enter in a TextBox. My KeyDown event is:

private void textBox_Zakljucak_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Tab))
    {
        Parent.SelectNextControl(textBox_Zakljucak, true, true, true, true);
    }
    else if ((e.KeyCode == Keys.Back))
    {
        textBox_Zakljucak.Select(textBox_Zakljucak.Text.Length, 0);
    }
    else if (!Regex.IsMatch(textBox_Zakljucak.Text, @"^[0-9.-]+$"))
    {
        textBox_Zakljucak.Clear();
        textBox_Zakljucak.Select(textBox_Zakljucak.Text.Length, 0);
    }
}
janw
  • 8,758
  • 11
  • 40
  • 62
user1788654
  • 311
  • 2
  • 5
  • 13
  • possible duplicate of [How to prevent the beep sound caused by alt key pressed in a WinForms TextBox?](http://stackoverflow.com/questions/722372/how-to-prevent-the-beep-sound-caused-by-alt-key-pressed-in-a-winforms-textbox) – nawfal Feb 06 '14 at 14:09

8 Answers8

34

You have to prevent the KeyPressed event from being generated, that's the one that beeps. That requires setting the SuppressKeyPress property to true. Make that look similar to:

if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Tab))
{
    Parent.SelectNextControl(textBox_Zakljucak, true, true, true, true);
    e.Handled = e.SuppressKeyPress = true;
}
janw
  • 8,758
  • 11
  • 40
  • 62
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    And what about if there was something like this: MessageBox.Show("Still beep!"); before Parent.[...]? The beep will still occur then. – Paweł Poręba Sep 16 '15 at 11:21
  • 5
    That's inevitable, the message box dispatches the KeyPressed event before you can cancel it. Don't use MessageBox or show it delayed by using this.BeginInvoke(). – Hans Passant Sep 16 '15 at 11:25
5

If you want to prevent the event from bubbling up in Winforms or WPF/Silverlight, you need to set e.Handled to true from within the event handler.

Only do this if you have actually handled the event to your satisfaction and do not want any further handling of the event in question.

user
  • 6,897
  • 8
  • 43
  • 79
2

this works for me.

private void txtTextbox_KeyDown(object sender, KeyEventArgs e)
{
    //do somthing

    if(e.KeyCode==Keys.Enter)
    {
        e.Handled=true;
        e.SuppressKeyPress=true;
    }
}

private void txtTextbox_KeyUp(object sender, KeyEventArgs e)
{
    e.Handled=false;
    e.SuppressKeyPress=false;
}
2

Running VS 2015 here and the above answers did not work for me. In order to suppress the beep on a hard return (in both textboxes and checkboxes), I switched from the KeyDown event to the KeyPress event and did the following:

private void mTxtSrchStr1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Return)
    {
         this.sSearchFind();
         e.Handled = true;
    }
}

There is no e.SuppressKeyPress in the KeyPress event, but it is not needed there.

janw
  • 8,758
  • 11
  • 40
  • 62
Ted Roth
  • 21
  • 1
2

After much "mucking" around, I found that you must set the KeyPreview value on the form containing your controls to True (under Form Properties) then use the KeyDown event to test for your keystroke, and if it is detected assign True to both the e event args object's "Handled" and "SuppressKeys" properties.

The following is VB.NET code, but the same methodology works in C#. Notice that the action is a keypress on the form, not the control itself.

Private Sub ThisForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
    'turn off beep
    e.Handled = e.SuppressKeyPress = True
End If
janw
  • 8,758
  • 11
  • 40
  • 62
Chris Raisin
  • 384
  • 3
  • 7
1
private void txtMessage_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
        _sendMessage.PerformClick();
    } 
}       
janw
  • 8,758
  • 11
  • 40
  • 62
  • 2
    This question is from 3 years ago and was flagged as accepted. Your answer doesn't add anything new. – Takarii Aug 08 '16 at 13:16
1

Just set the form KeyPreview property to true, then add the following code to the form KeyPress event:

if (e.KeyChar == (char)Keys.Return)
     e.Handled = true;

Then the beep sound should be gone.

janw
  • 8,758
  • 11
  • 40
  • 62
0

Sure, just split into KeyDown and KeyUp:

private void txtUserName_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        #region (Disabled ... moved to txtUserName_KeyDown.)
        /*
        e.Handled = true;
        e.SuppressKeyPress = true;
        TimeStampPicker.txtUserName.Text = TimeStampPicker.txtUserName.Text.mzlxToUpper();
        TimeStampPicker.btnOK.Focus();
        //e.Handled = e.SuppressKeyPress = true;
        */
        #endregion
    }
    else
    {
        #region
        TimeStampPicker.txtUserName.Text = TimeStampPicker.txtUserName.Text.mzlxToUpper();
        TimeStampPicker.txtUserName.Select(TimeStampPicker.txtUserName.Text.Length, 0);
        #endregion
    }
}
private void txtUserName_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        #region
        e.Handled = true;
        e.SuppressKeyPress = true;
        TimeStampPicker.txtUserName.Text = TimeStampPicker.txtUserName.Text.mzlxToUpper();
        TimeStampPicker.btnOK.Focus();
        #endregion
    }
}
janw
  • 8,758
  • 11
  • 40
  • 62