Windows 窗体 TabControl 显示多个选项卡,这些选项卡类似于笔记本中的分隔卡和档案柜文件夹中的标签。选项卡中可包含图片和其他控件。TabControl 控件可用来产生多页对话框。这种对话框出现在 Windows 操作系统中的许多地方,如显示器控制面板中。
TabControl 控件最重要的属性是 TabPages,它包含单独的选项卡。每个单独的选项卡是一个 TabPage 对象。单击选项卡时,将为相应的 TabPage 对象引发 Click 事件。
更改 Windows 窗体 TabControl 控件的外观
可使用 TabControl 控件和在控件上组成各选项卡的 TabPage 对象的属性来更改 Windows 窗体中选项卡的外观。通过设置这些属性,可以编程方式在选项卡上显示图像,以垂直方式而非水平方式显示选项卡,创建多行选项卡,以及启用或禁用选项卡。
在选项卡的标签部位显示图标
1. 将 ImageList 控件添加到窗体中。
2. 将图像添加到图像列表中。
3. 将 TabControl 的 ImageList 属性设置为 ImageList 控件。
4. 将 TabPage 对象的 ImageIndex 属性设置为列表中的相应图像的索引。
创建多行选项卡
1. 添加所需数目的选项卡页。
2. 将 TabControl 控件的 Multiline 属性设置为 true。
3. 如果选项卡尚未以多行方式显示,则设置 TabControl 控件的 Width 属性,使其比所有的选项卡都窄。
在控件一侧排列选项卡
· 将 TabControl 控件的 Alignment 属性设置为 Left 或 Right。
以编程方式启用或禁用选项卡
· 将 TabPage 对象的 Enabled 属性设置为 true 或 false。
· ' Visual Basic
· TabPage1.Enabled = False
·
· // C#
· tabPage1.Enabled = false;
·
· // C++
tabPage1->Enabled = false;
将选项卡显示为按钮
· 将 TabControl 控件的 Appearance 属性设置为 Buttons 或 FlatButtons。
使用 Windows 窗体 TabControl 控件可以按有组织的方式显示其他控件。
在设计器中添加控件
1. 单击适当的选项卡页以使其显示在最前面。
2. 在选项卡页上绘制控件。
以编程方式添加控件
· 使用 TabPage 对象的 Controls 属性的 Add 方法:
· tabPage1.Controls.Add(new Button());
TabControl 包含选项卡页,这些选项卡页由通过 Controls 属性添加的 TabPage 对象表示。
下面的示例使用 Visual Studio .NET Windows 窗体设计器创建具有三个选项卡页的 TabControl。每个选项卡页包含若干控件。
public class Form1 : System.Windows.Forms.Form
{
// Required designer variable.
private System.ComponentModel.Container components;
// Declare variables.
private System.Windows.Forms.RadioButton tab3RadioButton2;
private System.Windows.Forms.RadioButton tab3RadioButton1;
private System.Windows.Forms.CheckBox tab2CheckBox3;
private System.Windows.Forms.CheckBox tab2CheckBox2;
private System.Windows.Forms.CheckBox tab2CheckBox1;
private System.Windows.Forms.Label tab1Label1;
private System.Windows.Forms.Button tab1Button1;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabControl tabControl1;
public Form1()
{
// This call is required for Windows Form Designer support.
InitializeComponent();
}
// This method is required for Designer support.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tab2CheckBox3 = new System.Windows.Forms.CheckBox();
this.tab3RadioButton2 = new System.Windows.Forms.RadioButton();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tab2CheckBox2 = new System.Windows.Forms.CheckBox();
this.tab2CheckBox1 = new System.Windows.Forms.CheckBox();
this.tab3RadioButton1 = new System.Windows.Forms.RadioButton();
this.tab1Label1 = new System.Windows.Forms.Label();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tab1Button1 = new System.Windows.Forms.Button();
tabPage1.Text = "tabPage1";
tabPage1.Size = new System.Drawing.Size(256, 214);
tabPage1.TabIndex = 0;
tab2CheckBox3.Location = new System.Drawing.Point(32, 136);
tab2CheckBox3.Text = "checkBox3";
tab2CheckBox3.Size = new System.Drawing.Size(176, 32);
tab2CheckBox3.TabIndex = 2;
tab2CheckBox3.Visible = true;
tab3RadioButton2.Location = new System.Drawing.Point(40, 72);
tab3RadioButton2.Text = "radioButton2";
tab3RadioButton2.Size = new System.Drawing.Size(152, 24);
tab3RadioButton2.TabIndex = 1;
tab3RadioButton2.Visible = true;
tabControl1.Location = new System.Drawing.Point(16, 16);
tabControl1.Size = new System.Drawing.Size(264, 240);
tabControl1.SelectedIndex = 0;
tabControl1.TabIndex = 0;
tab2CheckBox2.Location = new System.Drawing.Point(32, 80);
tab2CheckBox2.Text = "checkBox2";
tab2CheckBox2.Size = new System.Drawing.Size(176, 32);
tab2CheckBox2.TabIndex = 1;
tab2CheckBox2.Visible = true;
tab2CheckBox1.Location = new System.Drawing.Point(32, 24);
tab2CheckBox1.Text = "checkBox1";
tab2CheckBox1.Size = new System.Drawing.Size(176, 32);
tab2CheckBox1.TabIndex = 0;
tab3RadioButton1.Location = new System.Drawing.Point(40, 32);
tab3RadioButton1.Text = "radioButton1";
tab3RadioButton1.Size = new System.Drawing.Size(152, 24);
tab3RadioButton1.TabIndex = 0;
tab1Label1.Location = new System.Drawing.Point(16, 24);
tab1Label1.Text = "label1";
tab1Label1.Size = new System.Drawing.Size(224, 96);
tab1Label1.TabIndex = 1;
tabPage3.Text = "tabPage3";
tabPage3.Size = new System.Drawing.Size(256, 214);
tabPage3.TabIndex = 2;
tabPage2.Text = "tabPage2";
tabPage2.Size = new System.Drawing.Size(256, 214);
tabPage2.TabIndex = 1;
tab1Button1.Location = new System.Drawing.Point(88, 144);
tab1Button1.Size = new System.Drawing.Size(80, 40);
tab1Button1.TabIndex = 0;
tab1Button1.Text = "button1";
tab1Button1.Click += new
System.EventHandler(this.tab1Button1_Click);
this.Text = "Form1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
// Adds controls to the second tab page.
tabPage2.Controls.Add(this.tab2CheckBox3);
tabPage2.Controls.Add(this.tab2CheckBox2);
tabPage2.Controls.Add(this.tab2CheckBox1);
// Adds controls to the third tab page.
tabPage3.Controls.Add(this.tab3RadioButton2);
tabPage3.Controls.Add(this.tab3RadioButton1);
// Adds controls to the first tab page.
tabPage1.Controls.Add(this.tab1Label1);
tabPage1.Controls.Add(this.tab1Button1);
// Adds the TabControl to the form.
this.Controls.Add(this.tabControl1);
// Adds the tab pages to the TabControl.
tabControl1.Controls.Add(this.tabPage1);
tabControl1.Controls.Add(this.tabPage2);
tabControl1.Controls.Add(this.tabPage3);
}
private void tab1Button1_Click (object sender, System.EventArgs e)
{
// Inserts the code that should run when the button is clicked.
}
public static void
Main(string[] args)
{
Application.Run(new Form1());
}
}
TabControl.SelectedIndexChanged 事件
这个事件很重要。