XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Topics

Ramblings

VB.NET

 Introduction
In the previous parts we looked at ways of hiding and showing a single Tab Page in a TabControl. In some situations you may need to hide and show multiple pages.

 This is still fairly easy to do, but now you need a collection in which to hold the TabPages you have removed. (Remember from the previous descriptions that you are not really hiding the TabPages, you are in fact completely removing them, but storing a copy in memory that you can later call on to reinstate the page)

A Collection of Saved TabPages
In VB2005 the availability of generic collections makes our life very easy and we can create type safe collections automatically. In this case we will create a List and this list will only allow TabPage objects to be stored in it. Here is the code that does this:

Code Copy
    '  A collection of removed TabPages
    Dim colRemovedTabs As New List(Of TabPage)

 Allowing User to Select
For demonstration purposes we need a way of listing all available TabPages. Whether this list is visible to the user and/or how it is displayed will depend on your particular application. For this article I will list all the TabPages in a standard ListBox control.. This list box will be populated when the form is first loaded:

Code Copy
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each tabpg As TabPage In TabControl1.TabPages
            Me.ListBox1.Items.Add(tabpg.Text)
        Next

        ListBox1.SelectionMode = SelectionMode.MultiSimple
    End Sub

  The final line of code inside that procedure is there simply to ensure that the user can select more than one TabPage at a time.

 Now the user can simply select multiple items (TabPages) from the listbox.

 Removing and Saving
Once they have made their selection, a button click is used to confirm that those pages should be hidden/removed. The click event code is as follows:

Code Copy
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        ' Hide multiple pages
        For Each tabpg As TabPage In TabControl1.TabPages
            For Each item As String In ListBox1.SelectedItems
                If tabpg.Text = Trim(item) Then
                    colRemovedTabs.Add(tabpg)
                    TabControl1.Controls.Remove(tabpg)
                End If
            Next
        Next

    End Sub

 All we have here is a nested loop. Firstly we loop through all of the TabPages in the TabControl. Inside that loop, we also loop through all the selected items in the listbox.
If we find a match between the selected item and the text (the string used on the Tab of a TabPage) then we know this item needs to be hidden..

 We store its information in the collection (colRemovedTabs) before removing it from the TabControl. The approach is very similar to that used in the earlier single TabPage examples.

 Reinstating Removed TabPages
Reinstating the TabPages is also an easy task. We have them all saved in the collection, so we can simply dip in and grab them all.

Once again I've placed this in a Button Click event for demo purposes:

Code Copy
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        '  Reinsert all removed pages
        For Each savedTab As TabPage In colRemovedTabs
            TabControl1.Controls.Add(savedTab)
        Next

        ' Clear the collection as it has now done its work
        colRemovedTabs.Clear()

    End Sub

  You will see that I have also cleared out the contents of the collection as the last code task. If you don't do this then further clicks of the button will result in duplicate TabPages being added back to the TabControl; probably not what you want in most situations.

Summary

It is also fairly easy to reinstate only certain hidden TabPages. The code for this is included in the demo solution which you can download from here.

 The completed project looks like this:

  So now you have seen how to "hide" and "show" individual or multiple TabPages. As I said at the start of the first part of these articles, this is one of those simple looking tasks that takes a bit more effort than you first think, but really isn't very difficult once you realise that you can work round the limitations.

posted on Wednesday, July 18, 2007 11:19 AM