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 have one or more of them. If you create only a tab control in your application, it is useless and does not 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 each a more meaningful name. 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 variable of type TabPage, allocate memory for it using the new operator, and add it to the Controls collection of its eventual tab control. Here is an example: public class Exercise : System.Windows.Forms.Form { TabControl tclPropertySheet; TabPage pgeController; public Exercise() { InitializeComponent(); } private void InitializeComponent() { Text = "Domain Configuration"; Size = new Size(345, 228); StartPosition = FormStartPosition.CenterScreen; tclPropertySheet = new TabControl(); tclPropertySheet.Location = new Point(14, 16); tclPropertySheet.Size = new Size(310, 170); pgeController = new TabPage(); tclPropertySheet.Controls.Add(pgeController); Controls.Add(tclPropertySheet); } } This would produce: Alternatively, you can declare a variable of type TabPage, allocate memory for it using the new operator, and add it to the TabPages collection of its tab control. Here is an example: public class Exercise : System.Windows.Forms.Form { TabControl tclPropertySheet; TabPage pgeController; TabPage pgeAccount; public Exercise() { InitializeComponent(); } private void InitializeComponent() { Text = "Domain Configuration"; Size = new Size(345, 228); StartPosition = FormStartPosition.CenterScreen; tclPropertySheet = new TabControl(); tclPropertySheet.Location = new Point(14, 16); tclPropertySheet.Size = new Size(310, 170); pgeController = new TabPage(); tclPropertySheet.Controls.Add(pgeController); pgeAccount = new TabPage(); tclPropertySheet.TabPages.Add(pgeAccount); Controls.Add(tclPropertySheet); } }
If you have added a tab 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-2013, FunctionX | Next |
|