Home

The Tab Pages of a Tab Control

 

Introduction

As mentioned previously, a tab control is made of tabs. These are the actual objects that make up a tab control. A tab control can one or more of them. If you create only a tab control in your application, it is useless and doesn't play its intended role. To make it useful, you must add property pages to it.

 

Creating a Tab Page

To support property pages, the .NET Framework provides the TabPage class. Because the property pages completely depend on the tab control, the TabPage class is not represented in the Toolbox. If you want to add a tab page to a tab control, the TabControl class is equipped with a property named TabPages, which is of type TabPageCollection. TabPageCollection is a collection class that implements the IList, the ICollection, and the IEnumerable interfaces.

If you create a tab control at design time by taking it from the Toolbox and adding it to a form, the tab control gets automatically equipped with two tabs. Here is an example:

At design time, to add a property page, you have various options:

  • If the tab control doesn't yet have any tabs, you can right-click its body and click Add Tab:
     
    Creating tab pages on a tab control

  • If the tab control already has one or more tabs, right-click the right side to its tabs and click Add Tab
     

  • If the tab control already has at least one tab, click the right side to the tabs to select the tab control. If the control doesn't yet have tabs, click its body to select it.
    When the tab control is selected, click the arrow button on its top-right side and click Add Tab
     

  • While the TabControl control is selected on the form, in the lower section of the Properties window, you can click the Add Tab link:
     

  • While the TabControl control is selected on the form, in the Properties window, click the TabPages field and click its ellipsis button Ellipsis to display the TabPage Collection Editor dialog box that allows you create and configure each page by clicking its Add button:
     

If you create the tab pages at design time, like all other controls, the names of tab pages are cumulative. As you add them, the first would be named tabPage1, the second would be named tabPage2, etc. If you plan to programmatically refer to the tab pages, you should give them more explicit names. As done with any other control, to set the name of a property page, after selecting it, in the Properties window, change the value of the (Name) field.

To programmatically create a property page, declare a handle to TabPage, allocate memory for it using the gcnew operator, and add it to the Controls collection of its eventual tab control. Here is an example:

public ref class CExercise : public Form
{
private:
    TabControl ^ tclPropertySheet;
    TabPage    ^ pgeController;

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        Size = System::Drawing::Size(345, 228);

        tclPropertySheet = gcnew TabControl;
        tclPropertySheet->Location = Point(14, 16);
	tclPropertySheet->Size = System::Drawing::Size(310, 170);

        pgeController = gcnew TabPage;
	tclPropertySheet->Controls->Add(pgeController);

        Controls->Add(tclPropertySheet);
    }
};

This would produce:

Alternatively, you can create a handle to TabPage, allocate memory for it using the gcnew operator, and add it to the TabPages collection of its tab control. Here is an example:

public ref class CExercise : public Form
{
private:
    TabControl ^ tclPropertySheet;
    TabPage    ^ pgeController;
    TabPage    ^ pgeAccount;

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        Size = System::Drawing::Size(345, 228);

        tclPropertySheet = gcnew TabControl;
        tclPropertySheet->Location = Point(14, 16);
	tclPropertySheet->Size = System::Drawing::Size(310, 170);

        pgeController = gcnew TabPage;
	tclPropertySheet->Controls->Add(pgeController);

        pgeAccount = gcnew TabPage;
	tclPropertySheet->TabPages->Add(pgeAccount);

        Controls->Add(tclPropertySheet);
    }
};

Removing a Tab Page

If you have added a page by mistake or you don't want a particular page anymore, you can remove it. To remove a property page, first click its tab to select it. Then,

  • You can right-click the TabControl control and click Remove Tab

  • You can select the tab control, click the arrow button on its top-right section and click Remove Tab

  • While the undesired tab page is selected on the tab control, you can press Delete

  • While the undesired tab page is selected on the tab control, in the lower section of the Properties window, you can click the Remove Tab link

  • You can right-click the body of the undesired page and click Delete

To programmatically delete a tab control, call the Remove() method of the TabPages property.

 

Previous Copyright © 2007 FunctionX, Inc. Next