Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I prevent the beep when the user presses the Enter key in a TextBox?

You can prevent the beep when the enter key is pressed in a TextBox by deriving the TextBox and overriding OnKeyPress.

[C#]

public class CustomTextBox : TextBox
{
  protected override void OnKeyPress( KeyPressEventArgs e )
  {
    if ( e.KeyChar == (char) 13 )
      e.Handled = true;
    else
      base.OnKeyPress( e );
  }
}

[Visual Basic]

Public Class CustomTextBox
  Inherits TextBox
  Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
    If e.KeyChar = CChar(13) Then
      e.Handled = True
    Else
      MyBase.OnKeyPress(e)
    End If
  End Sub
End Class

Contributed from George Shepherd's Windows Forms FAQ



Page view counter