Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable beep of enter and escape key c#

Tags:

c#

enter

beep

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);
            }
    }
like image 876
user1788654 Avatar asked Dec 19 '12 12:12

user1788654


2 Answers

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;
        }
like image 138
Hans Passant Avatar answered Sep 19 '22 15:09

Hans Passant


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.

like image 23
user Avatar answered Sep 17 '22 15:09

user