標籤:winform style color os width art
建立一個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)
- {
- //this.tabControl1.SelectedIndex = 1;
- }
-
- 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.SelectedIndex = index;
- this.tabControl1.SelectedTab = Page;
- //this.tabControl1.SelectTab("Page" + index.ToString());
- #endregion
-
- index++;
- }
-
- private void btnRemove_Click(object sender, EventArgs e)
- {
- bool first = true;
- if (index > 0)
- {
- #region 兩種刪除某個選項卡的方法
- this.tabControl1.Controls.RemoveAt(this.tabControl1.SelectedIndex);
- //this.tabControl1.Controls.Remove(this.tabControl1.TabPages[this.tabControl1.TabPages.Count-1]);
- #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";//修改當前選項卡的屬性
- //this.tabControl1.SelectedTab.Name = "";
- //this.tabControl1.SelectedTab.Tag = "";
- //this.tabControl1.SelectedTab.Select();
- }
- }