Question

I made a quick web browser in vb.net, I have it so that when you press enter it navigates to the webpage in textbox1. The only thing is that it beeps everytime I press enter. I tried using e.Handled = True, but it hasn't done anything. Here is my code for the keypress

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.Handled = True
        WebBrowser1.Navigate(TextBox1.Text)
    End If

End Sub

I thought e.Handled would have made that annoying beep go away, but it hasn't.

Was it helpful?

Solution

The KeyEventArgs Property that you are wanting is not Handled but SuppressKeyPress.

i.e.

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
        WebBrowser1.Navigate(TextBox1.Text)
    End If

End Sub

From the first MSDN Link:

Handled is implemented differently by different controls within Windows Forms. For controls like TextBox which subclass native Win32 controls, it is interpreted to mean that the key message should not be passed to the underlying native control. If you set Handled to true on a TextBox, that control will not pass the key press events to the underlying Win32 text box control, but it will still display the characters that the user typed.

If you want to prevent the current control from receiving a key press, use the SuppressKeyPress property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top