program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace GroupBoxTest13 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); //Appearance 获取或设置控件选项卡的可视外观 //HotTrack 获取或设置一个值,该值指示鼠标移到控件的选项卡时,这些选项卡是否更改外观 //ImageTrack 获取或设置在控件的选项卡上显示的图像 //Multiline 获取或设置一个值,该值指示是否可以显示一行以上的选项卡 //SelectedIndex 获取或设置当前选定的选项卡页 //TabCount 获取选项卡条中选项卡的数目 //TabPages 获取该选项卡控件中选项卡的集合 //DeselectTab 使指定的选项卡后面的选项卡成为当前选项卡 //RemoveAll 从该选项卡控件中移除所有的选项卡和附加的控件 //SelectTab使指定的选项卡成为当前选项卡 } } }
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace GroupBoxTest13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { tabControl1.Appearance = TabAppearance.Normal; //设置选项卡的外观样式 } private void button1_Click(object sender, EventArgs e) { //声明一个字符串变量,用于生成新增选项卡的名称 string Title = "新增选项卡" + (tabControl1.TabCount + 1).ToString(); TabPage MyTabPage = new TabPage(Title); //使用TabControl控件的TabPages属性的add方法添加新的选项卡 tabControl1.TabPages.Add(MyTabPage); MessageBox.Show("现有" + tabControl1.TabCount + "个选项卡"); //获取选项卡的个数 } private void button2_Click(object sender, EventArgs e) { if(tabControl1.SelectedIndex==0) { MessageBox.Show("情选择要移除的选项卡"); } else { //使用TabControl控件的TabPages属性的Remove方法移除指定的选项卡 tabControl1.TabPages.Remove(tabControl1.SelectedTab); } } } }
Form1设计:
namespace GroupBoxTest13 { partial class Form1 { /// <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> 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.button2 = 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(12, 12); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(341, 128); this.tabControl1.TabIndex = 0; // // tabPage1 // this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); this.tabPage1.Size = new System.Drawing.Size(333, 102); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "tabPage1"; this.tabPage1.UseVisualStyleBackColor = true; // // tabPage2 // this.tabPage2.Location = new System.Drawing.Point(4, 22); this.tabPage2.Name = "tabPage2"; this.tabPage2.Padding = new System.Windows.Forms.Padding(3); this.tabPage2.Size = new System.Drawing.Size(192, 74); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "tabPage2"; this.tabPage2.UseVisualStyleBackColor = true; // // button1 // this.button1.Location = new System.Drawing.Point(151, 150); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 1; this.button1.Text = "添加选项卡"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(253, 150); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(96, 23); this.button2.TabIndex = 2; this.button2.Text = "移除选项卡"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(365, 185); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.tabControl1); this.Name = "Form1"; this.Text = "选项卡"; this.Load += new System.EventHandler(this.Form1_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; private System.Windows.Forms.Button button2; } }
C#-TabControl---ShinePans,布布扣,bubuko.com
时间: 2024-12-12 05:41:39