workaround: hiding tabpages on a tabcontrol

K

Kevin

Although lots of people described how to workaround the lack of ability
to hide and show tab pages on a tab control, I couldnt find a code
example so I made one. Create a class that inherits from the
tabcontrol and then add the following

Private htVisibleVal As New Hashtable
Private htVisibleKey As New Hashtable
Private htInvisibleKey As New Hashtable

'stores the current tabpages
Public Sub Initialise()
Dim tp As TabPage
Dim i As Integer
htVisibleVal.Clear()
htVisibleKey.Clear()
htInvisibleKey.Clear()

For i = 0 To Me.TabPages.Count - 1
htVisibleKey.Add(Me.TabPages(i), i)
htVisibleVal.Add(i, Me.TabPages(i))
Next

End Sub

'removes the tabpage from the tabpages collection and visible
hashtables and adds it to the invisible hashtable
Public Sub HideTabPage(ByVal tpIn As TabPage)

Dim tp As TabPage
Dim i As Integer = htVisibleKey(tpIn)
htVisibleKey.Remove(tpIn)
htVisibleVal.Remove(i)
htInvisibleKey.Add(tpIn, i)
Me.TabPages.Remove(tpIn)
End Sub
'moves the tabpage from the invisible hashtable to the visible one.
Then iterates through the visible hashtable
'adding them back to the tabcontrol
Public Sub ShowTabPage(ByVal tpIn As TabPage)

Dim i As Integer = htInvisibleKey(tpIn)
htInvisibleKey.Remove(tpIn)
htVisibleKey.Add(tpIn, i)
htVisibleVal.Add(i, tpIn)

Me.TabPages.Clear()
For i = 0 To htVisibleVal.Count - 1
Me.TabPages.Add(htVisibleVal(i))
Next
End Sub

call the initialise method after the windows designer has added the tab
pages to its tabpage collection. You only need to do this once.

It could probably use some exception catching but hopefully it will get
you started.
 
K

Kevin

Thanks for your reply Cor, that is a useful site, however it didnt
contain anything about storing the position of a tab page when you
'hide' so that it can be added in the same position afterwards. The
insert tab function is useful though.

Also a slight tweak to my code, the loop in ShowTabPage should be:

For i = 0 To htVisibleVal.Count + htInvisibleKey.Count - 1
If htVisibleVal.Contains(i) Then
Me.TabPages.Add(htVisibleVal(i))
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top