supress beep

Pankaj tripathi 185 Reputation points
2023-11-17T13:17:45.0366667+00:00
 private static void Txb_KeyDown(object sender, KeyEventArgs e)
        {
            var txb = (TextBox)sender;
            var intfc = (ICustomTextBox)sender;
            if (e.KeyCode == Keys.Escape)
            {
                txb.Text = "";
                return;
            }
            if (!intfc.Last)
            {
                if (e.KeyCode == Keys.Enter) 
                {

                   Control ctl = txb;
                    while (ctl.Parent != null)
                    {
                        var next = ctl.Parent.GetNextControl(ctl, true);
                        if (next != null)
                        {
                            next.Focus();
                            e.SuppressKeyPress = true;
                            e.Handled = true;
                            return;
                        }
                        ctl = ctl.Parent;
                    }
                }                
            }
          
            if (e.KeyData == (Keys.Control | Keys.V) ||
                e.KeyData == (Keys.V | Keys.Alt | Keys.Control))
            {
                txb.SelectedText = Clipboard.GetText();
                e.SuppressKeyPress = true;
                e.Handled = true;
                return;
            }

        }


in the Txb_KeyDown event ,as e.SuppressKeyPress = true; is preventing the system to generate the beep sound when the user presses enter key , i am trying that beep sound should not happen for any of the keys or keys combination . i had tried putting

e.SuppressKeyPress = true;

in this event but instead , it blocks the user input .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,853 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,516 Reputation points Microsoft Vendor
    2023-11-17T14:41:05.73+00:00

    Hi @Pankaj tripathi , Welcome to Microsoft Q&A,

    If you want to disable the beep when the bool variable last is set to true and the user presses the enter key.

    Your problem may be one of judgment. Try using

      // Check if the user pressed Enter and last is true
         if (e.KeyCode == Keys.Enter && intfc.Last)
         {
             e.SuppressKeyPress = true;
             e.Handled = true;
             return;
         }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.