C#中TabControl控件应用实例

来源:互联网 发布:淘宝双十一直播 编辑:程序博客网 时间:2024/06/05 21:47
TabControl控件
功能
TabControl控件显示多个选项卡,这些选项卡类似于笔记本中的分隔卡和档案柜文件夹中的标签。TabControl控件的选项卡中可包含图片和其他控件。此外,TabControl控件还可以用来创建一组相关属性的属性页。图1所示为TabControl控件。

图1  TabControl控件
2.属性
TabControl控件常用属性及说明如表1所示。

表1   TabControl控件常用属性及说明
下面详细介绍TabPages属性,此属性获取该选项卡控件中选项卡页的集合。
语法:
public TabPageCollection TabPages { get; }
属性值:TabControl.TabPageCollection,它包含该TabControl中的TabPage对象。
说明:此集合中的选项卡页的顺序反映了选项卡在控件中出现的顺序。
示例本教程来自http://www.isstudy.com/
TabPages属性的使用
本示例中,当程序运行时,单击【TabPages 属性】按钮,向TabPages控件中添加项。示例运行结果如图2所示。

图2  TabPages属性
程序主要代码如下:
string strName = "功能" + Convert.ToString(this.tabControl1.TabPages.Count + 1);
string strTap = "tabPage" + Convert.ToString(this.tabControl1.TabPages.Count);
this.tabControl1.TabPages.Add(strTap, strName);
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_48
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmTabControl());
}
}
}
★★★★★frmTabControl窗体设计文件完整程序代码网站源代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_48
{
public partial class frmTabControl : Form
{
public frmTabControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.tabPage1.Text = "功能1";
this.tabPage2.Text = "功能2";
string strName = "功能" + Convert.ToString(this.tabControl1.TabPages.Count + 1);
string strTap = "tabPage" + Convert.ToString(this.tabControl1.TabPages.Count);
this.tabControl1.TabPages.Add(strTap, strName);
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void frmTabControl_Load(object sender, EventArgs e)
{
}
}
}
★★★★frmTabControl窗体代码文件完整程序代码★★★★★
namespace _8_48
{
partial class frmTabControl
{
/// <summary>
/// 必需的设计器变量。本教程来自http://www.isstudy.com/
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//

this.tabControl1.Controls.Add(this.tabPage1);

this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(53, 38);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(271, 127);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(263, 102);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
this.tabPage1.Click += new System.EventHandler(this.tabPage1_Click);
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 21);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(263, 102);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// button1
//本教程来自http://www.isstudy.com/
this.button1.Location = new System.Drawing.Point(53, 193);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(154, 23);
this.button1.TabIndex = 1;
this.button1.Text = "TabPages属性";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// frmTabControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(416, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "frmTabControl";
this.Text = "frmTabControl";
this.Load += new System.EventHandler(this.frmTabControl_Load);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button button1;
}
}
3.事件
Click事件在单击选项卡时发生。
示例
Click事件
本示例中,当程序运行时,单击【第一选项卡】按钮,弹出对话框,显示“你好”字样。
程序主要代码如下:
private void tabPage1_Click(object sender, EventArgs e)
{
MessageBox.Show("你好");
}
完整程序代码如下:
★★★★★主程序文件完整程序代码网站源代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_48
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmTabControl());
}
}
}
★★★★frmTabControl窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_48
{
public partial class frmTabControl : Form
{
public frmTabControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.tabPage1.Text = "功能1";
this.tabPage2.Text = "功能2";
string strName = "功能" + Convert.ToString(this.tabControl1.TabPages.Count + 1);
string strTap = "tabPage" + Convert.ToString(this.tabControl1.TabPages.Count);
this.tabControl1.TabPages.Add(strTap, strName);
}
private void tabPage1_Click(object sender, EventArgs e)
{
MessageBox.Show("你好");
}
private void frmTabControl_Load(object sender, EventArgs e)
{
}
private void tabControl1_Click(object sender, EventArgs e)
{
MessageBox.Show("你好");
}
}
}
★★★★★frmTabControl窗体代码文件完整程序代码★★★★★
namespace _8_48
{
partial class frmTabControl
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。

/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>本教程来自http://www.isstudy.com/
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(53, 38);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(271, 127);
this.tabControl1.TabIndex = 0;
this.tabControl1.Click += new System.EventHandler(this.tabControl1_Click);
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(263, 102);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
this.tabPage1.Click += new System.EventHandler(this.tabPage1_Click);
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 21);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(263, 102);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(53, 193);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(154, 23);
this.button1.TabIndex = 1;
this.button1.Text = "TabPages属性";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// frmTabControl
//本教程来自http://www.isstudy.com/
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(416, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "frmTabControl";
this.Text = "frmTabControl";
this.Load += new System.EventHandler(this.frmTabControl_Load);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button button1;
}
}

转自:

http://www.isstudy.com/cjc/1462.html

http://www.isstudy.com/cjc/1462_2.html

http://www.isstudy.com/cjc/1462_3.html