Tabbed dialogs

From HashVB
Jump to: navigation, search
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

There are some nice UI features in Windows called tabbed dialogs. In VB, there are two built in ways of implementing your own: The SSTab control, also known as the Microsoft Tabbed Dialog Control (This used to be the Sheridan tab control), and the TabStrip, part of Windows Common Controls. Out of these two, the TabStrip is a much better option despite having to deal with switching tabs yourself.

Problems with the SSTab control

The SSTab control has a nice feature of allowing you to switch tabs and place controls at runtime, but this comes at a price. When you change tab, the controls aren't actually hidden, but moved a long way off to the left. This has the undesired side affect of keeping them in the tab order and accessible (although out of sight).

One container per tab

To get around the problems with the SSTab and to make the TabStrip usable, you will need to put each "page" of controls onto an individual frame control (with the border removed). You can then hide and show these as the relevant tab is selected.

This code will do the switching for you if you have a control array of Frame controls called "Tab" with indexes starting from 0 to the amount of tabs minus 1:

Private Sub Tabs_Click()
Static PreviousTab As Long

  'Hide the previous tab
  If PreviousTab <> 0 Then Me.Tab(PreviousTab - 1).Visible = False
  'Set the new "previous tab"
  PreviousTab = Tabs.SelectedItem.Index
  'Show it and bring it to the front
  Me.Tab(PreviousTab - 1).Visible = True
  Me.Tab(PreviousTab - 1).ZOrder 0
  'Set the help ContextID so the pressing F1 on the tab brings up the tabs help page
  Tabs.HelpContextID = Me.Tab(PreviousTab - 1).HelpContextID
End Sub

XP style tabs

With the prevalence of Windows XP and customisable window themes, You can make your Tab dialog use the same style, by making sure you use the VB5 version of the Windows Common Controls, and follow the rest of the instructions on applying the XP style to your application.

The only part of the tab you can't easily give an XP style is the background for the tabs as the controls aren't transparent. A work around is to detect the colour of a certain part of the tab control after showing the form and setting the background of all controls to this colour. You can also use some advanced hooking and drawing techniques available on this PSCode article.

See also