hannir hannir - 5 months ago 28
Vb.net Question

How to change text in textbox if webbrowser url changes?

What I have:

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


I just want to change the text in an textbox if the URL is curURL2. Is there an possibility to do it?

Greetz,
Hannir

Answer

You certainly do not need the While and Do loops. All you want is a simple series of If statements. But this needs to be done in the handler for the web browser's Navigated event, not in the Load event.

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
Comments