In the previous part I looked at a very simple scenario where the developer knew in advance which TabPage should be hidden and re-shown. Next we'll look at doing the same thing, except that this time the user gets to choose which TabPage is hidden.
As in the previous example, you're going to need some kind of trigger to kickstart the Hide or Show activity. I've used a button placed outside the TabControl on the form itself, but you could use any other trigger that suits your purpose - e.g. a button or checkbox inside a TabPage.

In fact of course it doesn't even have to be wired up to the clicking on a Windows Forms Control - you could hide the TabPage based on some kind of text input from the user.
Anyway, in this example, the button click code needs to :
- Identify which TabPage currently has focus
- Then store this TabPage's metadata in the TempTab variable
- Before finally Removing the TabPage from the TabControl.
Here is the code I've used:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Identify the TabPage to be removed
For Each tp As TabPage In Me.TabControl1.TabPages
If tp.Focus = True Then
TempTab = tp
TabControl1.TabPages.Remove(tp)
Exit For
End If
Next
End Sub
I think that code is pretty straightforward. To identify the single Tabpage that currently has focus you have to enumerate through all of them until you find the right one.
For Each tp As TabPage In Me.TabControl1.TabPages
If tp.Focus = True Then
Once you've located it, store it in TempTab;
then remove it:
TempTab = tp
TabControl1.TabPages.Remove(tp)
Finally (and crucially) don't forget to jump out of the loop once you have found your TabPage and taken the appropriate action on it.
Exit For
Why? Because what happens when you remove a Tabpage is that Focus automatically shifts to the first TabPage in the TabControl. Therefore if you don't jump out of the loop, it will keep finding TabPages that have focus, one after the other, and will keep removing them until all are gone. You won't get an Exception, but you won't have any TabPages either! (And only the most recently deleted one will be available for reshowing; you will have permanently lost all the others.)
OK, the code to show the TabPage again is simple:
TabControl1.TabPages.Add(TempTab)
As mentioned earlier, you will get unwanted results if you allow this button to be clicked more than once consecutively, so I advise that you add further code to avoid this happening. I will post up a downloadable sample project that includes this, but for conciseness won't deal with it in this blog.
So that would seem to fix the "Remove user's choice of TabPage" problem easily enough. But what if for some reason it's important to reinstate the saved tabPage back in the TabControl in its original position?
All sorts of ways of doing this spring to mind, but just because I want to keep this part really uncomplicated, I'm simply going to set up a second variable, call it TempTabIndex and make it an Integer Type.
We will save the TabPage to TempTab as before and save the index value to TempTabIndex.
Here's the code:
' Identify the Index Number of the selected TabPage
With TabControl1
For i As Integer = 0 To .TabPages.Count - 1
If .TabPages(i).Focus = True Then
' Save the details of this TabPage
TempTab = .TabPages(i)
' Save its index number
TempTabIndex = i
' Remove it
.TabPages.RemoveAt(i)
' Stop looking for Tabpages with Focus
Exit For
End If
Next
End With
As you can see, instead of enumerating through the TabPages what we do here is iterate through the TabControl's TabPages collection, an index at a time.
Once we find the TabPage that has focus, we can also easily grab the current index number and store that, as well as storing the tabPage data in the same way as we did previously.
And again as in the earlier example, it's important to ensure that you jump out of that loop as soon as you've completed the work on the one TabPage we're interested in.
Reinserting the TabPage back into its correct original position simply requires us to pass in the Index number and the TabPage name to the TabControl's.TabPages Insert method:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
' Reinsert TabPage in correct position
TabControl1.TabPages.Insert(TempTabIndex, TempTab)
End Sub
The project described in these first two parts now looks something like this:

In the next part I want to look at a more complex situation; one where more than one TabPage is hidden at a time.
Font>