TabControl
ControlThe TabControl
creates tabbed windows, such as those in Visual Studio (Fig. 15.32). This enables you to specify more information in the same space on a Form
and group displayed data logically. TabControl
s contain TabPage
objects, which are similar to Panel
s and GroupBox
es in that TabPage
s also can contain controls. You first add controls to the Tab-Page
objects, then add the TabPage
s to the TabControl
. Only one TabPage
is displayed at a time. To add objects to the TabPage
and the TabControl
, write
myTabPage.Controls.Add(myControl);
myTabControl.TabPages.Add(myTabPage);
The preceding statements call method Add
of the Controls
collection and method Add
of the TabPages
collection. The example adds TabControl
myControl to TabPage
myTab-Page, then adds myTabPage to myTabControl. Alternatively, we can use method AddRange
to add an array of TabPage
s or controls to a TabControl
or TabPage
, respectively. Figure 15.33 depicts a sample TabControl
.
You can add TabControl
s visually by dragging and dropping them onto a Form
in Design mode. To add TabPage
s in Design mode, click the top of the TabControl
, open its smart tasks menu and select Add Tab (Fig. 15.34). Alternatively, click the TabPages
property in the Properties window and add tabs in the dialog that appears. To change a tab label, set the Text
property of the TabPage
. Clicking the tabs selects the TabControl
—to select the TabPage
, click the control area underneath the tabs. You can add controls to the TabPage
by dragging and dropping items from the ToolBox. To view different TabPage
s, click the appropriate tab (in either design or run mode).
Common properties and a common event of TabControl
s are described in Fig. 15.35. Each TabPage
generates a Click
event when its tab is clicked. Event handlers for this event can be created by double clicking the body of the TabPage
.
TabControl
properties and an event.TabControl properties and an event |
Description |
---|---|
Common Properties | |
ImageList |
Specifies images to be displayed on tabs. |
ItemSize |
Specifies the tab size. |
Multiline |
Indicates whether multiple rows of tabs can be displayed. |
SelectedIndex |
Index of the selected TabPage . |
SelectedTab |
The selected TabPage . |
TabCount |
Returns the number of tab pages. |
TabPages |
Returns the collection of TabPage s within the TabControl as a TabControl.TabPageCollection . |
Common Event | |
SelectedIndexChanged |
Generated when SelectedIndex changes (i.e., another TabPage is selected). |
Class UsingTabsForm
(Fig. 15.36) uses a TabControl
to display various options relating to the text on a label (Color, Size and Message). The last TabPage
displays an About message, which describes the use of TabControl
s.
The textOptionsTabControl
and the colorTabPage
, sizeTabPage
, messageTabPage
and aboutTabPage
are created in the designer (as described previously):
The colorTabPage
contains three RadioButton
s for the colors black (blackRadioButton
), red (redRadioButton
) and green (greenRadioButton
). This Tab-Page
is displayed in Fig. 15.36(a). The CheckedChanged
event handler for each RadioButton
updates the color of the text in displayLabel
(lines 22, 29 and 36).
The sizeTabPage
(Fig. 15.36(b)) has three RadioButton
s, corresponding to font sizes 12 (size12RadioButton
), 16 (size16RadioButton
) and 20 (size20-RadioButton
), which change the font size of displayLabel
—lines 44, 52 and 60, respectively.
The messageTabPage
(Fig. 15.36(c)) contains two RadioButton
s for the messages Hello! (helloRadioButton
) and Goodbye! (goodbyeRadioButton
). The two RadioButton
s determine the text on displayLabel
(lines 67 and 74, respectively).
The aboutTabPage
(Fig. 15.36(d)) contains a Label
(messageLabel
) describing the purpose of TabControl
s.
A TabPage
can act as a container for a single logical group of RadioButton
s, enforcing their mutual exclusivity. To place multiple RadioButton
groups inside a single TabPage
, you should group RadioButton
s within Panel
s or GroupBox
es contained within the TabPage
.