Hello, I am building a webbrowser using VB.Net 2010, and I am having trouble updating the url when I switch between open tabs using tabcontrol. I am also using a textbox for the address bar. Any help would be appreciated Thanks!

Recommended Answers

All 15 Replies

Use the .Tag Property of the selectedTab when navigating to a site, to store the URL in. When switching between tabs, retrieve the .Tag of the selectedTab and display it in your TextBox. Hope this helps.

If you are trying to save the navigated URL of the webbrowser before switching tabs, you could try something like this:

Add this on the TabControl1_Selected Event

Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        If TabControl1.SelectedTab.Text = "TabPage2" Then
            txtURL.Text = WebBrowser1.Url.ToString
        End If
    End Sub

What this does is save the WebBrowser's URL then inputs it into the URL field. If this is not what you are wanting to do then you can try to explain yourself in a much clearer way.

What do you mean by .Tag property? I am unfamiliar with that one. Could you please go into a little more detail on that? Thanks

thing2, what exactly are you wanting to accomplish? If you are just trying to save the WebBrowser's URL when you switch tabs then have it display in the URL box then my code should work just fine.

I am just trying to update the url in the address bar when I switch between tabs. For instance, if one tab has yahoo, and the other google when I switch between them I want the address bar to update accordingly.

Try this in a new project with a TabControl containing 2 TabPages.

Public Class Form1
   
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With TabControl1
            .TabPages(0).Tag = "hi"
            .TabPages(1).Tag = "hello"
        End With
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        With TabControl1.SelectedTab
            If Not .Tag Is Nothing Then MsgBox(.Tag)
        End With
    End Sub
End Class

All you have to do everytime you navigate a wb on a tabPage, set the .Tag of the .SelectedTab to the url you are navigating to.
Some webpages might change urls once your wb has finished loading the website, therefore it would be wise to set the .Tag in the wb_DocumentCompleted event as well.

That worked as a test, but the msgbox isn't needed. I'm guessing you included that as a way to show the results, so I will implement this into my existing code and let you know how it turns out. Thanks again.

:)

I don't think either of these solutions will work. Msgbox isn't the same as an address bar, and I copied and pasted your code Netjunkie and nothing changed...tabcontrol1.selectedindex accomplishes the job of switching between 0 and 1 but doesn't actually use the url. Neither selectedindex nor selectedtab allow me to use url.tostring however.

Let's take baby steps.
1st. How are you navigating to a site for a selectedTab, or how are you getting the url into your TextBox from the selectedTab's WebBrowser? Post code.

This is the code inside my go button event handler

CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(TextBox1.Text)
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle

Here is the code for my add tab button:

Dim browser As New WebBrowser
        browser.Name = "New Tab"
        browser.Dock = DockStyle.Fill
        TabControl1.TabPages.Add("New Tab" & i + 1)
        TabControl1.SelectTab(i)
        TabControl1.SelectedTab.Controls.Add(browser)
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://oceanviewnewtab.webs.com")
        AddHandler browser.ProgressChanged, AddressOf bar_loading
        AddHandler browser.DocumentCompleted, AddressOf done
        i = i + 1
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle

Go btn:

With TabControl1.SelectedTab
            CType(.Controls.Item(0), WebBrowser).Navigate(TextBox1.Text)
            .Text = CType(.Controls.Item(0), WebBrowser).DocumentTitle
            .Tag = TextBox1.Text '// Add URL to .Tag of SelectedTab.
        End With

Add Tab btn:

Private myHomepageURL As String = "http://oceanviewnewtab.webs.com"

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        With TabControl1
            Dim browser As New WebBrowser With {.Name = "New_Tab", .Dock = DockStyle.Fill}
            .TabPages.Add("New Tab" & i + 1)
            .SelectTab(i)
            .SelectedTab.Controls.Add(browser)
            AddHandler browser.ProgressChanged, AddressOf bar_loading
            AddHandler browser.DocumentCompleted, AddressOf done
            CType(.SelectedTab.Controls.Item(0), WebBrowser).Navigate(myHomepageURL)
            .SelectedTab.Tag = myHomepageURL '// Add URL to new tab's.Tag.
            i += 1
            .SelectedTab.Text = CType(.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
        End With
    End Sub

TabControl:

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        With TabControl1.SelectedTab
            If Not .Tag Is Nothing Then TextBox1.Text = .Tag
        End With
    End Sub

Let me know if this takes care of the issue or if we need to take some more baby steps.:D

sorry but that didn't do the job. It actually didn't change the results at all.

If you add the code I have to the TabControl_Selected Event then when you select the tab, the URL of the WebBrowser with export to the URL text box.

You can also add this code after the URL.ToString to navigate to the URL:

WebBrowser1.Navigate(txtURL.Text)

So your new code, if you add this onto the code I provided before, should look like this:

Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        If TabControl1.SelectedTab.Text = "TabPage2" Then
            txtURL.Text = WebBrowser1.Url.ToString
            WebBrowser1.Navigate(txtURL.Text)
        End If
    End Sub

Hope this helps and if not, we can try to work on it using a different method. ;)

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.