[RESOLVED] [2008] Stop Beep sound when pressing Enter on TextBox
Hello to the community,
My working environment:
OS : Vista
VS :2008
If I press Enter key when a TextBox has focus, a BEEP sound plays.
I want to prevent this sound.
( I want to handle this key by KeyDown event to go next control using Control.Select)
Please help me
Re: [2008] Stop Beep sound when pressing Enter on TextBox
You can try Console.Beep which beeps through the computer speaker if there is one.
http://msdn.microsoft.com/en-us/library/8hftfeyw.aspx
Re: [2008] Stop Beep sound when pressing Enter on TextBox
your best bet is to set the forms keypreview property to True and handle the event at the form level in the KeyPress event. To stop the beep you add the line e.Handled = True
an Example:
Code:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = ControlChars.Cr Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, True, True, False, True)
End If
End Sub
Re: [2008] Stop Beep sound when pressing Enter on TextBox
You handle the KeyPress event of the textbox, and test if e.KeyChar = Chr(13) then set e.Handled = True and select the next control in tab order.
Something like this:
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
e.Handled = True 'This prevents the beep to sound
Me.SelectNextControl(CType(sender, Control), True, True, True, True)
End If
End Sub
Edit: Bmahler beats me again :)
Re: [2008] Stop Beep sound when pressing Enter on TextBox
Oh wow, yet again I failed. Completly misread the question.
sorry!
Re: [2008] Stop Beep sound when pressing Enter on TextBox
Thanks to all of you.
I am using KeyDown event to handle Enter key and move to next control
and here is what I am able to to resolve it like:
vb Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Me.TextBox2.Select()
End If
End Sub