如果webbrowser url发生更改,如何更改文本框中的文本?

时间:2016-06-23 14:23:44

标签: vb.net

我有什么:

Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

WebBrowser1.Navigate(curURL)

    While WebBrowser1.Url Is curURL
        Do
            TextBox1.Text = "1/5"
        Loop
    End While
    While WebBrowser1.Url Is curURL2
        Do
            TextBox1.Text = "2/5"
        Loop
    End While

End Sub

如果网址为curURL2,我只想更改文本框中的文字。是否有可能这样做?

格尔茨, Hannir

1 个答案:

答案 0 :(得分:1)

你当然不需要While和Do循环。你想要的只是一系列简单的If语句。但这需要在Web浏览器Navigated event的处理程序中完成,而不是在Load事件中。

Private Sub form1_Load(ByVal sender As System.Object, _
                       ByVal e As System.EventArgs)   _
                       Handles MyBase.Load

    ' When the form loads, navigate to the first URL
    WebBrowser1.Navigate(curURL)

End Sub


Private Sub WebBrowser1_Navigate(ByVal sender As System.Object,           _
                                 ByVal e as WebBrowserNavigatedEventArgs) _
                                 Handles WebBrowser1.Navigated

    ' Check the URL that was just navigated to, and see if we find a match.
    Dim navigatedUrl As String = e.Url.ToString()
    If navigatedUrl = curURL
        TextBox1.Text = "1/5"
    Else If navigatedUrl = curURL2
        TextBox1.Text = "2/5"
    Else If ...

    Else
        TextBox1.Text = "???"
    End If

End Sub