70-526 MCTSTabControl controlThe TabControl control is a container control that allows you to display multiple dialogs on a single form by switching between the tabs. This is analogous to using dividers in a notebook. Each tab acts in a similar manner to a Panel control and is its own type of object called a TabPage. These tabs are located in the collection stored in the TabPages property of the TabControl. Inheritance hierarchySystem.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.TabControl
Useful properties
![]()
Useful methodsAdding and Deleting TabsTabs can be added or deleted by selecting the TabControl and clicking on the
Smart Tag icon ![]() Editing the TabPages CollectionThe TabPages collection can be edited using the TabPage Collection Editor. ![]() This editor gives access to the properties of the individual TabPage objects. Adding a TabPage manuallyThe following code sample shows very simply how to add a TabControl with two TabPage objects. private void AddTabControl()
{
TabControl tabControl1 = new TabControl();
TabPage tabPageUser = new TabPage("User Details");
TabPage tabPageAccount = new TabPage("Account Details");
tabControl1.TabPages.Add(tabPageUser);
tabControl1.TabPages.Add(tabPageAccount);
tabControl1.Location = new Point(20, 20);
this.Controls.Add(tabControl1);
}
Disabling Tab PagesA TabPage does not have an Enabled property that does anything meaningful. Therefore in order to disable a TabPage, the SelectedIndexChanged event must be handled. The easiest option for handling his event is to assign the SelectedTab property of the TabControl to another TabPage. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if( tabControl1.SelectedTab == tabPage2 )
tabControl1.SelectedTab = tabPage1;
}
Hiding Tab PagesA TabPage does not have a Visible property that does anything meaningful. Therefore in order to hide/show a TabPage, the TabPage must be removed: tabControl2.TabPages.Remove(tabPage4); or added accordingly: tabControl2.TabPages.Add(tabPage4); Render Right-Aligned or Left-Aligned Tabs horizontallyWhen you set the Alignment property to either Left or Right, the tabs appear vertically. By drawing the tabs yourself, you can have horizontal text tabs. To achieve this follow these simple steps.
When the form runs, horizontal text is in the tabs. ![]() MSDN references
|