title: Vb.net How to programically select the last tab in a tabcontrol
body: <p>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 &quot;SelectedIndex&quot; but that doesn't seem the best way to change the selected tab.</p>
  <p><em><strong>In Summmary When I click New Tab it...</strong></em></p>
  <ol>
  <li>Creates a new tab at the END of the tab control with the url set to about:blank</li>
  <li>Doesn't change the selected tab to the &quot;newly created tab&quot;</li>
  </ol>
  <p>Here is the code of the New Tab button</p>
  <pre><code>Private Sub btn_NewTab_Click(sender As Object, e As EventArgs) Handles btn_NewTab.Click
      AddTab(&quot;about:blank&quot;, TabControl1) 
  End Sub
  </code></pre>
  <p>The AddTab Sub code is below</p>
  <pre><code>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
  </code></pre>
  <p>If you need to look at more of the code then he is a link to all of the code behind the web browser
  <a href="http://lobsterproductions.weebly.com/uploads/1/9/5/1/1951779/tabbed_webbrowser.txt" rel="nofollow noreferrer">Full Code</a></p>
  <p>Update: I Have tried adding</p>
  <blockquote>
  <p>TabControl.SelectedIndex = TabControl.TabPages.Count - 1</p>
  </blockquote>
  <p>to the AddTab sub and I get an error that highlights</p>
  <blockquote>
  <p>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</p>
  <pre><code>End Sub
  </code></pre>
  </blockquote>