XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

AfterNote:   There are now three blog items on this topic, each showing a different aspect  If you want to view them all as one article, follow this link)

 Sometimes you may want to hide a TabPage from the user's view, but you don't want to permanently remove it.   Now, if you've checked out the TabPage's methods you will see that it does have a "Hide" method in VB 2005. That's great, but the trouble is if you try using it, it doesn't work!   You won't get an Exception, but you won't get a disappearing Tab Page either. (And if you're using an earlier version of VB.NET you have a choice of Hide or the Visible property, but neither will have the effect you want if you try using them.)

 Actually, the way to hide a TabPage is to access it through its parent container - the TabControl itself. We'll take a look at how to do this. (and possibly how to sidestep other problems along the way).

 Let's start with the situation where you know that you are only going to want to hide and show one specific TabPage in a TabControl which has a set number of TabPages.

First, you need somewhere to store the info about the TabPage. In this scenario a simple variable will do:-

Code Copy
    '  A temp variable to hold a single hidden Page
    Dim TempTab As TabPage

The code goes at the top of the form, outside any procedures.

Next, in the event you are using to trigger the hide action, (maybe a button click - see the "Hide TabPage 2" button in the screenshot below),

Simple preselected TabPage

you store the chosen TabPage in that TempTab variable.
And finally you use the Remove or RemoveAt method to physically delete the TabPage from within the TabControl.

Code Copy
TempTab = TabControl1.TabPages(1)
TabControl1.TabPages.RemoveAt(1)

Because we knew in advance that we would be hiding and removing the second TabPage (whose Index is 1, of course), we can use the RemoveAt method. This is the most straightforward approach to use in this situation. So the situation currently is that the TabPage no longer exists in the TabControl, but you have squirrelled away a copy in that TempTab variable for later use.

What we need to look at next is reinstating the hidden TabPage when needed. Again this is quite simple. We insert the TabPage back into the TabControl, taking care to insert it in its proper original position.

Code Copy
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TabControl1.TabPages.Insert(1, TempTab)
  End Sub

The parameters of that Insert method are the Index to Insert at (Index 1, in this case, as that's where it came from) and the TabPage itself, as identified by its name. (Don't confuse its name with its Text. In this example they happen to be the same, but in a real world example this is unlikely to be the case).

If you're wondering about the purpose of the TextBox in that screenshot, I put it in so you could really prove to yourself that this does work.

If you create a Form as shown and enter some text into the TextBox at runtime and then Save/Remove the TabPage you want to be sure that any data input by the user really does get saved too. So, run the project, enter your name, hit the first button to hide the TabPage, then hit the second one to show it again. As you will see, even dynamically entered data, such as the text in the textBox, is saved and reinstated.

To re-emphasise the point I made earlier about the Name of the TextBox being used as the key parameter, not the Text that the user sees on the Tab, take a look at the following screenshot:

 TabPage Text Changed

The same code as used above will work perfectly with these TabPages, because I have only changed the Text and not the Name.

By the way, if you don't need or want to reinsert the TabPage back in the position you took it out from, you can simply have it placed at the end of the current collection by using:

    TabControl1.TabPages.Add(TempTab)

instead.

Added at end

However, one little Gotcha to watch out for (it's obvious when you think about it, but ...) is that if you use the "Show" button more than once then you will get multiple TabPages added; each of which has the same Text and - more importantly - each of which has the same Name.
However, not so obvious, and even worse, is that this multiplicity of Tabpages with the same name gives the compiler such a headache that in fact you will lose all the controls and formatting from all copies of that TabPage. Not something that you would normally want to happen, so you should include code to restrict the Hiding/Showing to single instances.

So far, so good. In the next part we'll look at a slightly more complex scenario.

posted on Monday, June 25, 2007 11:10 AM