Reputation: 31283
I'm trying to enable tabs functionality in the WebBrowser control. I have a TabControl hosting WebBrowser controls in each tab. And a multiline enables Textbox to enter the sites. It looks something like this,
An this is the code I have.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m As String()
Dim w As New WebBrowser
Dim i As Integer
TabControl1.TabPages.RemoveAt(0)
m = TextBox1.Text.Split(Environment.NewLine)
For Each k As String In m
TabControl1.TabPages.Add(i, k.Trim)
TabControl1.SelectedTab.Controls.Add(w)
w.Dock = DockStyle.Fill
w.Navigate(k)
i = i + 1
Next
End Sub
End Class
Its supposed to take each string(URL) and execute them in separate tab. It opens up the correct number of tabs according to the user input. However it only executes the last URL.
I think I have an idea as to what's wrong here. This line
TabControl1.SelectedTab.Controls.Add(w)
It adds the WebBrowser control to the TabControl at the SelectedTab position. Since the default selected tab is the first, it only adds it to the first tab therefore executing only that browser.
I want to know how I can select the next tab from the loop as the SelectedTab so that when the loop runs again and again, it would keep adding WeBrowser controls to each tab.
Upvotes: 1
Views: 2981
Reputation: 2313
In your example you need to move the creation for 'w
' (WebBrowser
) into the For Each
loop. You are using one instance of 'w
' that is essentially moved into between the tabs whilst the loop executes.
You want something like this
For Each k As String In m
Dim w As New WebBrowser() ' <-- Move the construction of WebBrower into the loop
TabControl1.TabPages.Add(i, k.Trim)
TabControl1.SelectedTab.Controls.Add(w)
w.Dock = DockStyle.Fill
w.Navigate(k)
i = i + 1
Next
Also I'm unsure if TabPages.Add
will update SelectedTab
, you may want to consider constructing new instances of TabPage
explicitly, within you loop to ensure you add the WebBrowser to the appropriate TabPage.
Dim tabPage As New TabPage(url) ' <-- 'k' in your example
tabPage.Controls.Add(w)
w.Dock = DockStyle.Fill
w.Navigate(url)
TabControl1.TabPages.Add(tabPage) ' <-- Add the tabPage to the TabControl
Upvotes: 1