![]() |
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. |
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 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);
}
};
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,
To programmatically delete a tab control, call the Remove() method of the TabPages property. |
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Next |
|
|
||