Reputation: 1
I am trying to create a browser in Visual Studio 2013 using windows forms and the browser needs to have an Address bar that doubles as a Google Search bar like in Chrome. Here is my code for the Address bar, but I do not know what to put in after "If" and before "Then" in order to accomplish this. Any ideas?
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If Then
AxWebBrowser1.Navigate(TextBox1.Text)
Else
AxWebBrowser1.Navigate("http://www.google.com/search?q=" + TextBox1.Text)
End If
End Sub
Upvotes: 0
Views: 1163
Reputation: 224963
You can use System.Uri.TryCreate
to check that; I’d use UriKind.Absolute
, though, because a lot of things are valid relative URIs.
Dim uri As Uri
If System.Uri.TryCreate(TextBox1.Text, UriKind.Absolute, uri) Then
' Navigate to it
Else
' Treat it as a search
End If
You could also make it a guess-free experience by requiring (or allowing) a prefix like ?
, which is easily checked with s.StartsWith("?")
and removed with s.Substring(1)
.
I just noticed the Ax
prefix; if you’re using an ActiveX control,
You can use System.Uri.IsWellFormedUriString
with the same first two parameters to just do a check instead of also creating a URI
Don’t
Upvotes: 1