Search the web from the address bar

前端 未结 2 713
傲寒
傲寒 2021-01-16 22:29

on my WebBrowser I have a address bar of course, but I can only search urls in the address bar, like \"facebook.com\" and something. I want my address field to be able to wo

2条回答
  •  执笔经年
    2021-01-16 22:49

    Maybe this is not the best way but it works.

    The only thing you need to do is add More things to the arrays to make it more prefect."because there are more possible web url endings and starts"
    Dim CheckArrStart()
    Dim CheckArrEnd()

    I would make a function to check to check the url entered in the address bar.
    Private Function CheckWebPageLink(ByVal Link As String) As Boolean

    In this function i would use 2 for each loops to check the start en the end of the url.
    If i find a start i would + 1 to my ok dim "Dim Ok As Integer = 0" and remove it from the string
    Then go to the next for each loop to find a url end.
    So if a find a end + 1 to my ok dim and also remove the end from the string. i do this because i want to check how long the string is at the end becaus there must be something between www. and .com

    Then at the end of my function i check if my ok dim has a value of 2 and how long the link is without the start and the end.
    If true i return a true boolean and if false i return a false boolean.

    Then in my navigate button i make a if statement end check if the booblean from my function is true ore false."If CheckWebPageLink(ToolStripTextBox1.Text.ToLower) = True Then"
    If true just navigate to the url and if false :
    WebBrowser1.Navigate("https://www.google.com/?gws_rd=ssl#q=" & ToolStripTextBox1.Text)

    Like i already said the arrays you still need to fix, i just added some to test.

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
    
        If CheckWebPageLink(ToolStripTextBox1.Text.ToLower) = True Then
            WebBrowser1.Navigate(ToolStripTextBox1.Text)
        Else
            WebBrowser1.Navigate("https://www.google.com/?gws_rd=ssl#q=" & ToolStripTextBox1.Text)
        End If
    
    End Sub
    
    Private Function CheckWebPageLink(ByVal Link As String) As Boolean
    
        Dim CheckArrStart() As String = ({"http://www.", "https://www.", "www."})
        Dim CheckArrEnd() As String = ({".se", ".nl", ".com", ".cu", ".tk"})
        Dim Ok As Integer = 0
    
        For Each LinkStart As String In CheckArrStart
            If Link.Contains(LinkStart) Then
                Link = Replace(Link, LinkStart, "")
                Ok = Ok + 1
                For Each LinkEnd As String In CheckArrEnd
                    If Link.Contains(LinkEnd) Then
                        Link = Replace(Link, LinkEnd, "")
                        Ok = Ok + 1
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
    
        If Ok = 2 And Link.Count > 2 Then
            Return True
        Else
            Return False
        End If
    
    End Function
    

提交回复
热议问题