I am working on a web browser for fun and to get a little more acquainted with VB. I felt lost during school this past year and wanted to do something that sounded like fun. Ok, enough of that.

When I put in my code to just press enter after inserting the desired URL nothing happens. It worked the first time I did it then I started adding code for other things and now it won't work. Here is my code.

Private Sub txtNav_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            WebBrowser1.Navigate(txtNav.Text)
        End If
    End Sub

I don't understand why it would just not work now after it worked before adding other coding for buttons etc. Any help would be appreciated. I don't want code per se, just an idea of where I went wrong and if there is something else to be added to make it work. It's really just a simple browser I am doing.

Thanks.

Dani AI

Generated

A short clarification and a few practical tips that expand on the thread: correctly identified the root cause — the event method had lost its connection to the TextBox, so pressing Enter produced no effect. In VB.NET an event handler is not invoked unless it’s wired to the control (either by a Handles clause in the method signature or by attaching it at runtime). That explains why the code worked before and stopped after further edits: renaming controls, moving methods between files, or copying/pasting can silently break the wiring.

Common editing pitfalls to check:

  • Confirm the KeyDown entry for the textbox in the designer (Properties → Events) is still set.
  • Search Designer.vb (or the InitializeComponent code) for any runtime AddHandler lines or for mismatched control names after a rename.
  • Look for other handlers that might set e.Handled/e.SuppressKeyPress or swallow the Enter key.
  • Use a breakpoint inside the handler to confirm whether it runs when Enter is pressed.

A robust runtime pattern (attach-if-needed and normalize URLs before navigating):

' attach the handler at runtime if wiring was lost or control is dynamic
AddHandler txtNav.KeyDown, AddressOf NavKeyHandler

' example: validate/normalize the address before navigating
Dim target As Uri = Nothing
If Not Uri.TryCreate(txtNav.Text.Trim(), UriKind.Absolute, target) Then
    Uri.TryCreate("http://" & txtNav.Text.Trim(), UriKind.Absolute, target)
End If
If target IsNot Nothing Then
    WebBrowser1.Navigate(target)
End If

Notes and best practices: trim whitespace, guard against invalid URIs to avoid exceptions, consider KeyPress vs KeyDown depending on needs, and set e.SuppressKeyPress/e.Handled in the handler to prevent system beeps or duplicate actions. ’s suggestion to construct a Uri is a good step toward making navigation resilient. ’s follow-up confirms that restoring the event hookup resolved the issue.

Recommended Answers

All 3 Replies

Handles clause is missing.

Private Sub txtNav_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNav.KeyDown
        If e.KeyCode = Keys.Enter Then
            WebBrowser1.Navigate(txtNav.Text)
        End If
    End Sub

try this

Private Sub txtNav_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNav.KeyDown
        If e.KeyCode = Keys.Enter Then
           WebBrowser1.Url = New System.Uri(txtNav.Text, System.UriKind.Absolute)
        End If
    End Sub

Yep it worked after putting in the Handles clause. I don't know how I missed that. I must have looked at it 100 times. Anyway, thanks for the help/replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.