如何搜索&从VB.net浏览器中的相同文本框导航

时间:2013-10-17 14:48:23

标签: vb.net visual-studio-2010 browser webbrowser-control vb.net-2010

我正在使用Visual Basic 2010中的Web浏览器,我想知道是否有任何方法可以在同一文本框中搜索和导航。我知道它可以完成,因为我之前已经看过它,但我不知道该怎么做。如果你不确定我在说什么,我说的是在你要搜索的网址栏中输入一些内容并点击输入搜索就像在chrome中一样,但也可以输入一个网址然后点击输入并转到那个网址。我已经有了文本框的keydown事件,它全部设置为导航但不搜索。什么是最好的搜索方式?

6 个答案:

答案 0 :(得分:0)

您需要以某种方式确定他们输入的内容是否与URL相似,如果没有,请构建一个不同的URL以导航到将其带到搜索引擎。

我很想推荐正则表达式,但之后你会有two problems

如果可以接受,更好的方法可能是拥有搜索令牌。例如,您可以检查第一个字符是否为?,然后将其用作是否搜索导航的触发器。

? This will get searched

但不幸的是,这对用户来说可能是直截了当的......

This won't search because I'm missing the '?'

答案 1 :(得分:0)

我实际上找到了一种更好的方法。令牌是个好主意,但很容易忘记。我现在这样做的方式:

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的内容。如果你找到了什么,请告诉我,以便我可以解决它。

感谢Jeff Bridgman提供了改进的代码。

答案 2 :(得分:0)

如果你不想使用令牌,你可能最好让.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

答案 3 :(得分:0)

以下是代码,您希望他们扫描网址:

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

答案 4 :(得分:0)

以下是代码,您希望他们扫描网址: 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

End If

答案 5 :(得分:0)

这是文本框的代码(我做了一些更改,因此它可以使用文本框而不是组合框)

    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