Member Avatar for Member #758415

Sorry about asking all of these questions, but I am making an app and I have had a request for when the user types into the text box he can automatically hit Enter and it will navigate to the specified destination. However I have tried EVERYTHING and I cannot get it to stay default when the user clicks the text box. So it's like this.
The user clicks the text box.
The GO button does not stay default.
How can I get it like this? Please help.

Dani AI

Generated

Short answer: set the form's AcceptButton (or, if you need special handling, catch Enter on the textbox and call the same navigation routine the button uses).

As demonstrated, intercepting Enter on the textbox works. An easier, built-in way for WinForms is to make the GO button the form's default so Enter activates it from any single-line control:

' set in the designer or at runtime (for example in Form_Load)
Me.AcceptButton = Button1

AcceptButton fires only when the focused control does not itself accept Enter (so ensure the textbox is single-line; MultiLine = True or controls that process Enter will block it). Microsoft docs: Form.AcceptButton property.

If you need more control (multi-line textbox, conditional behavior, or you want to suppress the system beep), handle the textbox KeyDown and call a single shared routine that performs the navigation (so your click and Enter both reuse the same code):

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
        DoGo()   ' put the navigate logic here and call it from Button.Click too
    End If
End Sub

Using a shared method avoids duplicate logic and avoids relying on UI tricks like PerformClick. For web pages (ASP.NET) the equivalent is Panel.DefaultButton (or wiring JavaScript on the input) — see Panel.DefaultButton. Small caution: do not set the button's DialogResult to close the form unless that is intended. This approach should give the exact Enter behavior asked for.

Recommended Answers

All 2 Replies

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "http://daniweb.com"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub

    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 '// cancel beep noise.
            Button1.PerformClick() '// programatically press button1.
        End If
    End Sub
End Class

Since Textbox1 will probably not be multi-line, pressing the Enter Key will cause a "beep" sound. This beep sound is a Windows error sound to notify the user that the Textbox cannot accept the enter key for a new line.

As in the code posted, I have added a line of code to cancel out the beep noise.:)

Member Avatar for Member #758415

Thank you!

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.