I have a tabbed web browser its working fine , but i have a little problem and that is if i open a new tab is ok but if i go back to previous tab the urltextbox it have the url in the tab last opened tab.
so i am asking you for help if you have the solution

Dani AI

Generated

As suggested, updating the address box when the selected tab changes is the right place to fix this. The points below add practical hardening and UX improvements so the textbox always reflects the active tab's browser and avoids null-reference or timing issues.

A small helper to find the tab's browser and a safe SelectedIndexChanged handler:

Private Function GetBrowserFromTab(tp As TabPage) As WebBrowser
    If tp Is Nothing Then Return Nothing
    For Each c As Control In tp.Controls
        Dim wb As WebBrowser = TryCast(c, WebBrowser)
        If wb IsNot Nothing Then Return wb
    Next
    Return Nothing
End Function

Private Sub tabs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tabs.SelectedIndexChanged
    Dim wb = GetBrowserFromTab(tabs.SelectedTab)
    If wb Is Nothing OrElse wb.Url Is Nothing Then
        urlTextBox.Text = String.Empty
    Else
        urlTextBox.Text = wb.Url.AbsoluteUri
    End If
End Sub

Keep the address bar synced during navigation by wiring each browser's Navigated (or DocumentCompleted) event when the tab/browser is created. Navigated updates the Url property earlier; DocumentCompleted fires after full load and may fire multiple times for frames.

Private Sub AttachBrowserHandlers(wb As WebBrowser)
    AddHandler wb.Navigated, AddressOf Browser_Navigated
End Sub

Private Sub Browser_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs)
    Dim wb As WebBrowser = DirectCast(sender, WebBrowser)
    If tabs.SelectedTab IsNot Nothing AndAlso tabs.SelectedTab.Controls.Contains(wb) Then
        urlTextBox.Text = e.Url.AbsoluteUri
    End If
End Sub

Additional tips: store the browser reference in the TabPage.Tag when creating a tab to avoid scanning Controls; initialize new tabs to "about:blank" so Url is not Nothing; remove event handlers when closing tabs; on Enter in the address box route navigation to the active tab's browser; avoid calling ToString on a Nothing Url. These small checks eliminate the common symptom where the address box shows the last-opened tab's URL when switching back.

Recommended Answers

All 2 Replies

Hi

You could use the SelectedIndexChanged event of the TabControl to then find the the WebBrowser control on that tab page and read its Url property. Something like:

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged

    'Grab the Web Browser control on this tab and read its URL property
    'NOTE, only expect to get one browser control.
    For Each wb As WebBrowser In TabControl1.SelectedTab.Controls.OfType(Of WebBrowser)()
        urlTextBox.Text = wb.Url.ToString
    Next

End Sub

The above does assume that you would only have one WebBrowser control on each TabPage. There are other ways of finding the WebBrowser control if this is not the case.

HTH

thank you very much you helped me so much i wish the best for you bro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.