The TabControl Control


The TabControl provides an easy way of organizing a dialog into logical parts that can be accessed through tabs located at the top of the control. A TabControl contains TabPages that essentially work like a GroupBox control, in that they group controls together, although they are somewhat more complex.

Figure 14-22 shows the Options dialog in Word 2000 as it is typically configured, though it is a Danish language version of Office. Notice the two rows of tabs at the top of the dialog. Clicking each of them will show a different selection of controls in the rest of the dialog. This is a very good example of how to use a tab control to group related information, making it easier for the user to find the information he/she is looking for.

image from book
Figure 14-22

Using the tab control is easy. You simply add the number of tabs you want to display to the control's collection of TabPage objects and then drag the controls you want to display to the respective pages.

TabControl Properties

The properties of the TabControl (shown in the following table) are largely used to control the appearance of the container of TabPage objects, in particular the tabs displayed.

Name

Description

Alignment

This property controls where on the tab control the tabs are displayed. The default is at the top.

Appearance

The Appearance property controls how the tabs are displayed. The tabs can be displayed as normal buttons or with flat style.

HotTrack

If this property is set to true, the appearance of the tabs on the control changes as the mouse pointer passes over them.

Multiline

If this property is set to true, it is possible to have several rows of tabs.

RowCount

RowCount returns the number of rows of tabs currently displayed.

SelectedIndex

This property returns or sets the index of the selected tab.

SelectedTab

SelectedTab returns or sets the selected tab. Note that this property works on the actual instances of the TabPages.

TabCount

TabCount returns the total number of tabs.

TabPages

This property is the collection of TabPage objects in the control. Use this collection to add and remove TabPage objects.

Working with the TabControl

The TabControl works slightly differently from all other controls you've seen so far. The control itself is really little more than a container for the tab pages that is used to display the pages. When you double- click a TabControl in the Toolbox you are presented with a control that already has two TabPages added to it, as shown in Figure 14-23.

image from book
Figure 14-23

When the mouse moves over the control a small button with a triangle appears at the control's upper- right corner. When you click this button, a small window is unfolded. This is called the Actions Window and is designed to allow you to easily access selected properties and methods of the control. You may have noticed this earlier as many controls in Visual Studio include this feature, but the TabControl is the first of the controls in this chapter that actually allows you to do anything interesting in the Actions Window. The Actions Window of the TabControl allows you to easily add and remove TabPages at design time.

The procedure outlined in the preceding paragraph for adding tabs to the TabControl is provided in order for you to get up and running quickly with the control. If, on the other hand, you want to change the behavior or style of the tabs you should use the TabPages dialog — accessed through the button when you select TabPages in the Properties window. the TabPages property is also the collection used to access the individual pages on a tab control.

Once you've added the TabPages you need you can add controls to the pages in the same way you did earlier with the GroupBox.

In the following Try It Out, you create an example to demonstrate the basics of the control.

Try It Out – Working with Tab Pages

image from book

Follow these steps to create a Windows Application that demonstrates how to develop controls located on different pages on the tab control.

  1. Create a new Windows application called TabControl in the directory C:\BegVCSharp\ Chapter14.

  2. Drag a TabControl control from the Toolbox to the form. Like the GroupBox the TabControl is found on the Containers tab in the Toolbox.

  3. Find the TabPages property, and click the button to the right of it after selecting it, to bring up the dialog shown in Figure 14-24.

    image from book
    Figure 14-24

  4. Change the Text property of the tab pages to Tab One and Tab Two, respectively, and click OK to close the dialog.

  5. You can select the tab pages to work on by clicking on the tabs at the top of the control. Select the tab with the text Tab One. Drag a button on to the control. Be sure to place the button within the frame of the TabControl. If you place it outside then the button will be placed on the form rather than on the control.

  6. Change the name of the button to buttonShowMessage and the Text of the button to Show Message.

  7. Click on the tab with the Text property Tab Two. Drag a TextBox control onto the TabControl surface. Name this control textBoxMessage and clear the Text property.

  8. The two tabs should look as shown in Figure 14-25 and Figure 14-26.

    image from book
    Figure 14-25

    image from book
    Figure 14-26

Adding the Event Handler

You are now ready to access the controls. If you run the code as it is, you will see the tab pages displayed properly. All that remains to do to demonstrate the tab control is to add some code such that when users click the Show Message button on one tab, the text entered in the other tab will be displayed in a message box. First, you add a handler for the Click event by double-clicking the button on the first tab and adding the following code:

private void buttonShowMessage_Click(object sender, EventArgs e) { // Access the TextBox. MessageBox.Show(this.textBoxMessage.Text); }

How It Works

You access a control on a tab just as you would any other control on the form. You get the Text property of the TextBox and display it in a message box.

Earlier in the chapter, you saw that it is only possible to have one radio button selected at a time on a form (unless you put them in group boxes). the TabPages work in precisely the same way as group boxes and it is, therefore, possible to have multiple sets of radio buttons on different tabs without the need to have group boxes. Also, as you saw in the buttonShowMessage_Click method, it is possible to access the controls that are located on other tabs than the one that the current control is on.

The last thing you must know to be able to work with a tab control is how to determine which tab is currently being displayed. There are two properties you can use for this purpose: SelectedTab and SelectedIndex. As the names imply, SelectedTab will return the TabPage object to you or null if no tab is selected, and SelectedIndex will return the index of the tab or –1 if no tab is selected. It is left to you in Exercise 2 to use these properties.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net