The TabControl
The TabControl is another staple of Windows development-it groups controls into multiple "pages." The technique has become remarkably successful because it allows a large amount of information to be compacted into a small, organized space. It's also easy to use because it recalls the tabbed pages of a binder or notebook. Over the years, the tab control has evolved into today's form, which is sometimes called property pages.
In .NET, you create a TabControl object, which contains a collection of TabPage objects in the TabPages property. Individual controls are then added to each TabPage object. The example that follows shows the basic approach, assuming your form contains a TabControl called tabProperties.
TabPage pageFile = new TabPage("File Locations");
TabPage pageUser = new TabPage("User Information");
// Add controls to the tab pages.
// The code for creating and configuring the child controls is omitted.
pageUser.Controls.Add(txtFirstName);
pageUser.Controls.Add(txtLastName);
pageUser.Controls.Add(lblFirstName);
pageUser.Controls.Add(lblLastName);
tabProperties.TabPages.Add(pageFile);
tabProperties.TabPages.Add(pageUser);
The output for this code is shown in Figure 6-16.

Figure 6-16: The TabPage control
Note
Chapter 11 presents an example that allows you to add controls to a TabPage without needing to supply a Location property. Instead, the layout is managed automatically.
The TabControl is easy to work with, and usually configured at design time. Some of its members are described in Table 6-12. TabPage properties are shown in Table 6-13.
Table 6-12: TabControl Members
Member
Description
Alignment
Sets the location of the tabs. With very few exceptions, this should always be TabAlignment.Top, which is the standard adopted by almost all applications.
Appearance
Allows you to configure tabs to look like buttons that stay depressed to select a page. This is another unconventional approach.
HotTrack
When set to true, the text in a tab caption changes to a highlighted hyperlink style when the user positions the mouse over it.
ImageList
You can bind an ImageList to use for the caption of each tab page.
Multiline
When set to true, allows you to create a tab control with more than one row of tab pages.
Padding
Configures a minimum border of white space around each tab caption. This does not affect the actual tab control, but it is useful if you need to add an icon to the TabPage caption and need to adjust the spacing to accommodate it properly.
RowCount and TabCount
Retrieves the number of rows of tabs and the number of tabs.
SelectedIndex and SelectedTab
Retrieves the index number for the currently selected tab, or the tab as a TabPage object, respectively.
ShowToolTips
Enables or disables tooltip display for a tab. This property is usually set to false.
SizeMode
Allows you to configure tab captions to be a fixed size, expand to the width of the contents, or match the size of the contents.
SelectedIndexChanged event
Occurs when the SelectedIndex property changes, usually as a result of the user clicking on a different tab.
Table 6-13: TabPage Properties
Property
Description
ImageIndex
The image shown in the tab.
Text
The text shown in the tab.
ToolTipText
The tooltip shown when the user hovers over the tab, if the TabControl.ShowToolTips property is true. No ToolTipProvider is used.