How can I prevent the beep when enter is hit in textbox?

Platform: WinForms| Category: 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 MyTextBox : TextBox
{
	protected override void OnKeyPress(KeyPressEventArgs e)
	{
		if(e.KeyChar == (char) 13)
			e.Handled = true;
		else
			base.OnKeyPress (e);
	}
}
[VB.NET]
Public Class MyTextBox
	Inherits TextBox
   
	Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
		If e.KeyChar = CChar(13) Then
			e.Handled = True
		Else
			MyBase.OnKeyPress(e)
		End If
	End Sub ’OnKeyPress
End Class ’MyTextBox

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.