Neville Software
Neville Software

Reputation: 9

Vb.net How to programically select the last tab in a tabcontrol

I am making a Web Browser that has a TabControl and I can everything to work but when I click New Tab Button it creates a new tab with a web browser in it but I have to manually select that new tab to change the address. I want it so that when I click New Tab it redirects me automatically to the new tab not the current tab I was on. I have looked at "SelectedIndex" but that doesn't seem the best way to change the selected tab.

In Summmary When I click New Tab it...

  1. Creates a new tab at the END of the tab control with the url set to about:blank
  2. Doesn't change the selected tab to the "newly created tab"

Here is the code of the New Tab button

Private Sub btn_NewTab_Click(sender As Object, e As EventArgs) Handles btn_NewTab.Click
    AddTab("about:blank", TabControl1) 
End Sub

The AddTab Sub code is below

Public Sub AddTab(ByRef URL As String, ByRef TabControl As TabControl)
    Dim NewBrowser As New CustomBrowser
    Dim NewTab As New TabPage
    NewBrowser.Tag = NewTab
    NewTab.Tag = NewBrowser
    TabControl.TabPages.Add(NewTab)
    NewTab.Controls.Add(NewBrowser)
    NewBrowser.Dock = DockStyle.Fill
    NewBrowser.Navigate(URL)

End Sub

If you need to look at more of the code then he is a link to all of the code behind the web browser Full Code

Update: I Have tried adding

TabControl.SelectedIndex = TabControl.TabPages.Count - 1

to the AddTab sub and I get an error that highlights

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag Me.cbURL.Text = WB.Url.ToString

End Sub

Upvotes: -1

Views: 5965

Answers (5)

user16594857
user16594857

Reputation:

The question states that using SelectedIndex does not work, and the majority of answers thus far suggest selecting a TabPage by name using SelectTab(nawtabpage) for a TabPage that was just created. However, when you need to select a TabPage which already exists, from anywhere in e.g. Form1 (the TabControl is on Form1), you can use the following command:

   TabControl1.TabPages(0).Select()

where (0) is TabPage 0 in TabControl1. If there are 4 TabPages in TabControl1, then their indices are 0,1,2,3, so selecting the last tab in the control (like the question asks) is just a variation on a theme, i.e.:

   TabControl1.TabPages(3).Select()

Upvotes: 0

Programmer Sanshray
Programmer Sanshray

Reputation: 1

I use

Private Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add.Click Dim t As New TabPage Dim newtab As New tab_layout newtab.Show() newtab.AxWebBrowser1.RegisterAsBrowser = True newtab.Visible = True newtab.TopLevel = False newtab.Dock = DockStyle.Fill t.Controls.Add(newtab) Browser.TabControl1.TabPages.Add(t) Browser.TabControl1.SelectedTab = t End Sub

and

Private Sub tab_layout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AxWebBrowser1.Navigate(My.Settings.Home) End Sub

for the problems you have

maybe this will help you

Upvotes: 0

Tarak Bhavsar
Tarak Bhavsar

Reputation: 145

        Dim i As Integer
        i = yourForm.TabControl.TabPages.Count
        yourForm.TabControl.SelectedIndex = i - 1

Upvotes: 0

NoGall
NoGall

Reputation: 5246

You can use the TabControl.SelectTab method and set the selected tab to any one you want. More details here: http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selectedtab(v=vs.110).aspx

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

I don't wknow what was wrong with TabControl.SelectedIndex=TabControl.TabPages.Count-1, but you could also use TabControl.SelectTab:

Public Sub AddTab(ByRef URL As String, ByRef TabControl As TabControl)
    Dim NewBrowser As New CustomBrowser
    Dim NewTab As New TabPage
    NewBrowser.Tag = NewTab
    NewTab.Tag = NewBrowser
    TabControl.TabPages.Add(NewTab)
    NewTab.Controls.Add(NewBrowser)
    NewBrowser.Dock = DockStyle.Fill
    NewBrowser.Navigate(URL)

    TabControl.SelectTab(NewTab)
End Sub

Upvotes: 1

Related Questions