新建一个WinForm程序,拖一个TabControl(在tabPages属性里面将默认的两个TabPage删除)和三个Button(增加、删除、修改)
- public partial class Form1 : Form
- {
- private int index = 0;
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- }
-
- private void btnAdd_Click(object sender, EventArgs e)
- {
- TabPage Page = new TabPage();
- Page.Name = "Page" + index.ToString();
- Page.Text = "tabPage" + index.ToString();
- Page.TabIndex = index;
- this.tabControl1.Controls.Add(Page);
-
- #region 三种设置某个选项卡为当前选项卡的方法
-
- this.tabControl1.SelectedTab = Page;
-
- #endregion
-
- index++;
- }
-
- private void btnRemove_Click(object sender, EventArgs e)
- {
- bool first = true;
- if (index > 0)
- {
- #region 两种删除某个选项卡的方法
- this.tabControl1.Controls.RemoveAt(this.tabControl1.SelectedIndex);
-
- #endregion
- }
- else
- {
- return;
- }
-
- #region 用于设置删除最后一个TabPage后,将倒数第二个设置为当前选项卡
- if (first)
- {
- this.tabControl1.SelectedIndex = --index - 1;
- first = false;
- }
- else
- {
- this.tabControl1.SelectedIndex = index--;
- }
- #endregion
- }
-
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- this.tabControl1.SelectedTab.Text = "xyt";
-
-
-
- }
- }
C#中 选项卡(Tabcontrol)动态添加TabPage(获取或设置当前选项卡及其属性),布布扣,bubuko.com
C#中 选项卡(Tabcontrol)动态添加TabPage(获取或设置当前选项卡及其属性)
原文:http://www.cnblogs.com/zhangyonglvdaomei/p/3837846.html