title: Hiding and Showing TabPages in VB .NET - TabManager
body: <p>I try to hide and show tabpages dynamically at runtime. For this I converted the code of Emile from <a href="https://stackoverflow.com/questions/3365025/hiding-and-showing-tabpages-in-tabcontrol/18028241?noredirect=1#comment26375231_18028241">here</a> to vb.net and the resulted problem is that tabpages cannot be shown anymore after they were hidden. At least not if they were set to invisible somewhere else than where I am trying set them visible.</p>
  <h1>edit:</h1>
  <p>After a long discussion i finally came up with a working result. i corrected the calling procedures and now this code works. It can hide and show tabpages that reside on any form, just like the <a href="https://stackoverflow.com/questions/3365025/hiding-and-showing-tabpages-in-tabcontrol/18028241?noredirect=1#comment26375231_18028241">original version</a> does. Thanks go to user varocarbas..</p>
  <p>Hiding a tabpage:</p>
  <pre><code>clsTabManager.SetInvisible(tabPage)
  </code></pre>
  <p>Showing a tabpage (call from <em>any</em> class/form):</p>
  <pre><code>clsTabManager.SetVisible(FormWithTabControl.tabPage, FormWithTabControl.TabControl)
  </code></pre>
  <p>Showing a tabpage (call from Form where TabControl <em>resides</em>):</p>
  <pre><code>clsTabManager.SetVisible(tabPage, TabControl)
  </code></pre>
  <p>clsTabmanager:</p>
  <pre><code>Public Class clsTabManager
  
      Private Structure TabPageData
          Friend Index As Integer
          Friend Parent As TabControl
          Friend Page As TabPage
  
          Friend Sub New(index__1 As Integer, parent__2 As TabControl, page__3 As TabPage)
              Index = index__1
              Parent = parent__2
              Page = page__3
          End Sub
  
          Friend Shared Function GetKey(tabCtrl As TabControl, tabPage As TabPage) As String
              Dim key As String = &quot;&quot;
              If tabCtrl IsNot Nothing AndAlso tabPage IsNot Nothing Then
                  key = [String].Format(&quot;{0}:{1}&quot;, tabCtrl.Name, tabPage.Name)
              End If
              Return key
          End Function
      End Structure
  
      Private hiddenPages As New Dictionary(Of String, TabPageData)()
  
    
      Public Sub SetVisible(page As TabPage, parent As TabControl)
          If parent IsNot Nothing AndAlso Not parent.IsDisposed Then
              Dim tpinfo As TabPageData
              Dim key As String = TabPageData.GetKey(parent, page)
  
              If hiddenPages.ContainsKey(key) Then
                  tpinfo = hiddenPages(key)
  
                  If tpinfo.Index &lt; parent.TabPages.Count Then
                      parent.TabPages.Insert(tpinfo.Index, tpinfo.Page)
                  Else
                      ' add the page in the same position it had
                      parent.TabPages.Add(tpinfo.Page)
                  End If
  
                  hiddenPages.Remove(key)
              End If
          End If
      End Sub
  
      Public Sub SetInvisible(page As TabPage)
          If IsVisible(page) Then
              Dim tabCtrl As TabControl = DirectCast(page.Parent, TabControl)
              Dim tpinfo As TabPageData
              tpinfo = New TabPageData(tabCtrl.TabPages.IndexOf(page), tabCtrl, page)
              tabCtrl.TabPages.Remove(page)
              hiddenPages.Add(TabPageData.GetKey(tabCtrl, page), tpinfo)
          End If
      End Sub
  
      Public Function IsVisible(page As TabPage) As Boolean
          Return page IsNot Nothing AndAlso page.Parent IsNot Nothing
          ' when Parent is null the tab page does not belong to any container
      End Function
  
      Public Sub CleanUpHiddenPage(page As TabPage)
          For Each info As TabPageData In hiddenPages.Values
              If info.Parent IsNot Nothing AndAlso info.Parent.Equals(DirectCast(page.Parent, TabControl)) Then
                  info.Page.Dispose()
              End If
          Next
      End Sub
  
      Public Sub CleanUpAllHiddenPages()
          For Each info As TabPageData In hiddenPages.Values
              info.Page.Dispose()
          Next
      End Sub
  
  End Class
  </code></pre>