I am working on a web browser in Visual basic 2010 and I was wondering if there is any way I could search and navigate in the same textbox. I know it can be done because I have seen it done before but I do not know how to do it. If you are not sure what I am talking about, I am talking about typing something into the url bar that you want to search and hitting enter to search just like in chrome but also being able to type in a url and hit enter and go to that url. I already have the keydown event for the textbox and it is all set up to navigate but it don't search. What would be the best way to search?
6 回答
我实际上已经找到了一种更好的方法来做到这一点。令牌是个好主意,但很容易忘记。我现在这样做的方式:
Dim textArray = AddressBar.Text.Split(" ")
If (AddressBar.Text.Contains(".") AndAlso
Not AddressBar.Text.Contains(" ") AndAlso
Not AddressBar.Text.Contains(" .") AndAlso
Not AddressBar.Text.Contains(". ")) OrElse
textArray(0).Contains(":/") OrElse textArray(0).Contains(":\") Then
'We have an URL!
End If
如您所见,此方法检查正常 URL 中可能包含的所有内容,如果有,则浏览器导航,但如果没有,则浏览器搜索 google。我还没有找到任何可以搜索但浏览器认为是 URL 的内容。如果你发现了什么,请告诉我,这样我就可以修复它。
感谢 Jeff Bridgman 改进了代码。
如果您不想使用令牌,最好让 .NET 为您进行检查...
Try
Dim uri = New Uri(txtAddressBar.Text)
'Success? Then we have an URL!
Catch ex As UriFormatException
'Not a valid URL, try searching instead...
End Try
您需要以某种方式确定他们输入的内容是否看起来像 URL,如果不是,则构建一个不同的 URL 以导航到该 URL,从而将他们带到搜索引擎。
我很想推荐正则表达式,但是你会遇到两个问题。
如果可以接受,更好的方法可能是使用搜索令牌。例如,您可以检查第一个字符是否为 a ?
,然后将其用作是否搜索导航的触发器。
? This will get searched
但不幸的是,这对用户来说可能是直截了当的......
This won't search because I'm missing the '?'
这是代码,您希望他们扫描网址:
If ComboBox1.Text.StartsWith("www.") Or ComboBox1.Text.EndsWith(".com") Or ComboBox1.Text.EndsWith(".com.au") Or ComboBox1.Text.EndsWith(".uk") Or ComboBox1.Text.EndsWith(".weebly") Or ComboBox1.Text.EndsWith(".co") Or ComboBox1.Text.EndsWith(".nf") Or ComboBox1.Text.EndsWith(".sh") Then
ComboBox1.Items.Add(ComboBox1.Text)
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
Else
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://www.google.com.au/search?q=" & ComboBox1.Text)
End If
这是代码,您希望他们扫描网址:
If ComboBox1.Text.StartsWith("www.") Or ComboBox1.Text.EndsWith(".com") Or ComboBox1.Text.EndsWith(".com.au") Or ComboBox1.Text.EndsWith(".uk") Or ComboBox1.Text.EndsWith(".weebly") Or ComboBox1.Text.EndsWith(".co") Or ComboBox1.Text.EndsWith(".nf") Or ComboBox1.Text.EndsWith(".sh") Then
ComboBox1.Items.Add(ComboBox1.Text)
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
Else
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://www.google.com.au/search?q=" & ComboBox1.Text)
End If
这是文本框的代码(我对其进行了一些更改,因此它与文本框而不是组合框一起使用)
If Textbox1.Text.StartsWith("www.") Or Textbox1.Text.EndsWith(".com") Or
Textbox1.Text.EndsWith(".com.au") Or Textbox1.Text.EndsWith(".uk") Or
Textbox1.Text.EndsWith(".weebly") Or Textbox1.Text.EndsWith(".co") Or
Textbox1.Text.EndsWith(".nf") Or Textbox1.Text.EndsWith(".nl") Or
Textbox1.Text.EndsWith(".net") Or Textbox1.Text.EndsWith(".eu") Or
Textbox1.Text.EndsWith(".tk") Or Textbox1.Text.EndsWith(".sh") Then
WebBrowser1.Navigate(TextBox1.Text)
Else
WebBrowser1.Navigate("https://www.google.com.au/search?q=" & TextBox1.Text)
End If