Jump to content

a little bit of vb help

Jordessey

i am trying to build a tabbed web browser for uni and i cant work it out, when ever i try and open a new tab, it wont have a browser object in it. i will put the code below and if someone could help it would be much appreciated
 

Public Class Form1
    Dim i As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btn_addtab_Click(sender As Object, e As EventArgs) Handles btn_addtab.Click
        Dim browse As New WebBrowser
        TabControl1.TabPages.Add(1, "page" & i)
        TabControl1.SelectedTab.Controls.Add(browse)
        browse.Dock = DockStyle.Fill
        browse.Name = "wb"
        i = i + 1

    End Sub

    Private Sub btn_removetab_Click(sender As Object, e As EventArgs) Handles btn_removetab.Click
        If TabControl1.TabCount = 1 Then
            Me.Close()
        Else
            TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
            i = i - 1
        End If
    End Sub

    Private Sub btn_back_Click(sender As Object, e As EventArgs) Handles btn_back.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
    End Sub

    Private Sub btn_forward_Click(sender As Object, e As EventArgs) Handles btn_forward.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
    End Sub

    Private Sub btn_go_Click(sender As Object, e As EventArgs) Handles btn_go.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(TextBox1.Text)
    End Sub

    Private Sub btn_refresh_Click(sender As Object, e As EventArgs) Handles btn_refresh.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
    End Sub

    Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click

    End Sub
End Class

 

Link to comment
Share on other sites

Link to post
Share on other sites

The code you're using adds a new tab, then puts the browser object in the SelectedTab. The tab you add won't be the selected tab so you need assign the new tab to the selected tab.

Dim browse As New WebBrowser
browse.Dock = DockStyle.Fill
browse.Name = "wb"
' etc

TabControl1.TabPages.Add(1, "page" & i)
TabControl1.SelectedIndex = TabControl1.TabCount - 1
TabControl1.SelectedTab.Controls.Add(browse)

Note that this will change the selected tab on the user which you may or may not want. Another option is to use the TabPages property instead of the SelectedTab property. This way you don't care what the selected tab is.

Dim browse As New WebBrowser
browse.Dock = DockStyle.Fill
browse.Name = "wb"
' etc

TabControl1.TabPages.Add(1, "page" & i)

Dim lastTabIndex = TabControl1.TabCount - 1
TabControl1.TabPages(lastTabIndex).Controls.Add(browse)

Alternatively you can create a TabPage object, assign it the required values and add the browser object to it, then add that object to the TabControl. This is the option I would use as I find it to be clearer.

Dim browse As New WebBrowser
browse.Dock = DockStyle.Fill
browse.Name = "wb"
' etc

Dim tab As New TabPage()
tab.Name = i
tab.Text = "page" & i
' etc

tab.Controls.Add(browse)

TabControl1.TabPages.Add(tab)
  
' Optionally select the tab with one of the following options (ie: by tab object or by index)
TabControl1.SelectedTab = tab
TabControl1.SelectedIndex = TabControl1.TabCount - 1

 

Also, in the future, please use code tags.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×