[SOLVED] Tab not selecting after added in tabbed webbrowser

Hello,
I have this code

Public Class Form1
    Dim x, y As Integer
    Dim newpoint As New Point
    Dim int As Integer = 1


    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        x = Control.MousePosition.X - Me.Location.X
        y = Control.MousePosition.Y - Me.Location.Y
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            newpoint = Control.MousePosition
            newpoint.X -= x
            newpoint.Y -= y
            Me.Location = newpoint
            Application.DoEvents()
        End If
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles xlabel.Click
        Me.Close()
    End Sub

    Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles p1.Click
        Me.Close()
    End Sub

    Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles p1.MouseDown
        p1.BackColor = Color.Red
    End Sub
    Private Sub Panel3_Paint(sender As Object, e As PaintEventArgs) Handles Panel3.Paint

    End Sub
    Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles xlabel.MouseLeave
        p1.BackColor = Color.Maroon
    End Sub



    Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles xlabel.MouseMove
        p1.BackColor = Color.Red
    End Sub


    Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles min.MouseLeave
        p2.BackColor = Color.DimGray
    End Sub



    Private Sub Label2_MouseMove(sender As Object, e As MouseEventArgs) Handles min.MouseMove
        p2.BackColor = Color.Gray
    End Sub
    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles min.Click
        Me.MinimizeForm(Me)
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles plabel.Click
        If My.Settings.maximazed = False Then
            Me.maximizeForm(Me)
        ElseIf My.Settings.maximazed = True Then
            Me.normalForm(Me)
        End If
    End Sub

    Public Sub MinimizeForm(sender As Object)
        sender.WindowState = FormWindowState.Minimized
    End Sub

    Public Sub maximizeForm(sender As Object)
        sender.WindowState = FormWindowState.Maximized
        My.Settings.maximazed = True
    End Sub

    Private Sub Label3_MouseMove(sender As Object, e As MouseEventArgs) Handles plabel.MouseMove
        p4.BackColor = Color.Green

    End Sub

    Private Sub Label3_MouseLeave(sender As Object, e As EventArgs) Handles plabel.MouseLeave
        p4.BackColor = Color.Teal
    End Sub





    Private Sub Button2_Click(sender As Object, e As EventArgs)
Back:
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()



    End Sub


    Public Sub normalForm(sender As Object)
        sender.WindowState = FormWindowState.Normal
        My.Settings.Maximazed = False
    End Sub































    Private Sub Menu_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click 'Menu Button (OPTIONAL)
        If StartMenuPanel.Visible = False Then
            StartMenuPanel.Show()
        Else
            StartMenuPanel.Hide()
        End If
    End Sub

    Private Sub About_Click(sender As System.Object, e As System.EventArgs) Handles Button14.Click 'About Button
        MsgBox("Gipial Browser is an advanced browser made by Gerassimos Moutafis. Copyright Gerassimos Moutafis 2017 - Contact [email protected]", MsgBoxStyle.OkOnly, Title:="About") 'Shows a message box with information on the web browser
    End Sub

    Private Sub AddTab_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 'Add Tab Button
        Dim Browser As New WebBrowser
        TabControl1.TabPages.Add("New Tab")
        TabControl1.SelectTab(int)
        Browser.Name = "Browser"
        Browser.Dock = DockStyle.Fill
        TabControl1.SelectedTab.Controls.Add(Browser)
        AddHandler Browser.DocumentCompleted, AddressOf Done
        AddHandler Browser.ProgressChanged, AddressOf Loading
        Browser.ScriptErrorsSuppressed = True '<< Add this to stop getting Script Errors in browser
        Browser.Navigate("www.google.com")
        StartMenuPanel.Hide()
    End Sub

    Private Sub Loading(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserProgressChangedEventArgs) 'When a TAB is loading
        Try
            ProgressBar1.Maximum = e.MaximumProgress
            ProgressBar1.Value = e.CurrentProgress
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) 'When a TAB has done loading
        TextBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
        Try
            History.ListBox1.Items.Add(TimeOfDay.TimeOfDay.ToString & "   " & CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle & " - " & CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.Url.ToString)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub RemoveTab_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click 'Remove Tab Button
        If Not TabControl1.TabPages.Count = 1 Then
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
        End If
        StartMenuPanel.Hide()
    End Sub

    Private Sub SavePageAs_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click 'Save the webpage button
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).ShowSaveAsDialog()
        StartMenuPanel.Hide()
    End Sub

    Private Sub PageProperties_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click 'Show Page properties button
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).ShowPropertiesDialog()
        StartMenuPanel.Hide()
    End Sub

    Private Sub Back_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'Go Back Button
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).CanGoBack = True Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
        Else

        End If
        StartMenuPanel.Hide()
    End Sub

    Private Sub Forward_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Go Forward Button
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).CanGoForward = True Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
        Else

        End If
        StartMenuPanel.Hide()
    End Sub

    Private Sub Refresh_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click 'Refresh Button
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
        StartMenuPanel.Hide()
    End Sub

    Private Sub Stop_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click 'Stop Button
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).IsBusy = True Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
        Else

        End If
        StartMenuPanel.Hide()
    End Sub

    Private Sub Home_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click 'Home Button
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.google.com")
        StartMenuPanel.Hide()
    End Sub
    Private Sub NewWindow_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click 'New Window Button
        Dim NwWindow As New Form1
        NwWindow.Show()
        StartMenuPanel.Hide()
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).CanGoBack = True Then
            Button1.Enabled = True
        Else
            Button1.Enabled = False
        End If
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).CanGoForward = True Then
            Button2.Enabled = True
        Else
            Button2.Enabled = False
        End If
        If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).IsBusy = True Then
            Button4.Enabled = True
        Else
            Button4.Enabled = False
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        AddHandler CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentCompleted, AddressOf Done
        AddHandler CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).ProgressChanged, AddressOf Loading

    End Sub

    Private Sub Bookmarks_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click 'Bookmarks Button
        Bookmarks.Show()
        StartMenuPanel.Hide()
    End Sub

    Private Sub Developer_Click(sender As System.Object, e As System.EventArgs) Handles Button13.Click 'Developer Tools Button
        Developer.ShowDialog()
        StartMenuPanel.Hide()
    End Sub

    Private Sub History_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click 'History Button
        History.Show()
        StartMenuPanel.Hide()
    End Sub

    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click

        Dim Browser As New WebBrowser
        TabControl1.TabPages.Add("New Tab")
        TabControl1.SelectTab(int)
        Browser.Name = "Browser"
        Browser.Dock = DockStyle.Fill
        TabControl1.SelectedTab.Controls.Add(Browser)
        AddHandler Browser.DocumentCompleted, AddressOf Done
        AddHandler Browser.ProgressChanged, AddressOf Loading
        Browser.ScriptErrorsSuppressed = True '<< Add this to stop getting Script Errors in browser
        Browser.Navigate("www.google.com")
        StartMenuPanel.Hide()



    End Sub

    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
        If Not TabControl1.TabPages.Count = 1 Then
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
        End If
        StartMenuPanel.Hide()
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown 'Searches websites when the ENTER key is pressed
        If e.KeyData = Keys.Enter Then
            If TextBox1.Text.Contains(".") Then
                CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(TextBox1.Text)
                e.SuppressKeyPress = True
                TextBox1.Clear()
            Else
                CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://www.google.com/search?q=" & TextBox1.Text)
                e.SuppressKeyPress = True
                TextBox1.Clear()
            End If
        End If
    End Sub
End Class[/b]

and a tab controler for my webbrowser.
Button25 is the button that adds a new tab.
When I press it to open the third tab and select the tab ,my code breaks in the place:

If CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).CanGoBack = True Then
            Button1.Enabled = True
        Else

There is an other problem. The third tab isnt auto selected.
Here is a screenhost of this error:

Thanks

Like 0 Dislike

Hi @gerassimosm,
Thanks for clarifying, the screenshot doesn't seem to be working; so I'm still not entirely sure what the error is.

Although, from looking at the code, it seems that the "int" variable isn't being changed when adding or removing tabs (since it records the current number of added tabs), technically this makes the variable kinda obsolete seeing as though you could just use "TabControl1.TabCount()", so now the "AddTab_Click" event should look something like this:

Private Sub AddTab_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 'Add Tab Button
  Dim Browser As New WebBrowser
  TabControl1.TabPages.Add("New Tab")
  TabControl1.SelectTab(TabControl1.TabCount() - 1)
  Browser.Name = "Browser"
  Browser.Dock = DockStyle.Fill
  TabControl1.SelectedTab.Controls.Add(Browser)
  AddHandler Browser.DocumentCompleted, AddressOf Done
  AddHandler Browser.ProgressChanged, AddressOf Loading
  Browser.ScriptErrorsSuppressed = True '<< Add this to stop getting Script Errors in browser
  Browser.GoHome()
  StartMenuPanel.Hide()
End Sub

And then you can remove line 4, as it is no longer needed.
You may also need to move lines 170 and 171 into the following "Try" statement in the same event in order to prevent any other weird exceptions, like this:

Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) 'When a TAB has done loading
  Try
    TextBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
    TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
    History.ListBox1.Items.Add(TimeOfDay.TimeOfDay.ToString & "   " & CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle & " - " & CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.Url.ToString)
  Catch ex As Exception
  End Try
End Sub

Hope this helps! 1f609

Like 0 Dislike

It seems like my old code isn't all too functional (it's kinda gross to be honest, my bad - although in my excuse this was almost 5 years ago 1f61b), so seeing as though I have more time; I'll probably just re-make the tutorial and update it's structure and whatnot.

Like 0 Dislike

Now I can't open any other tab..   1f626

Like 0 Dislike

Try changing this:

TabControl1.SelectTab(TabControl1.TabCount())

To this, instead:

TabControl1.SelectTab(TabControl1.TabCount() - 1)

Like 0 Dislike