2011-03-30 150 views
2

嗨大家 我想問: 如何以編程方式添加選項卡。以編程方式添加選項卡,c#選項卡控件

我的問題 我有一個選項卡控件,作爲默認只有一個選項卡。我有一個按鈕,當我點擊該按鈕將添加一個其他選項卡。所以會是兩個標籤。

請幫助我使用c#和xaml。

+2

我還想說,我們許多人一樣我喜歡分享我的知識,幫助人們並獲得幫助,但這不是我的工作,所以我會鼓勵人們在谷歌之前提出一些問題... TabControl.Items.Add是您學習的第一件事情,如果您鍵入像「TabControl WPF tuto rial「與El goog ... – Damascus 2011-03-30 14:18:41

回答

6
tabControl.Items.Add(yourNewTabItem); 
0

試試這個方法:

tabControl1.TabPages.Add("tab 3"); 
0

一些更多的代碼手動創建和修改的TabPages:

public partial class Form1 : Form 
{ 
    TabControl tc; 
    public Form1() 
    { 
     InitializeComponent(); 
     tc = new TabControl(); 

     tc.TabPages.AddRange(new TabPage[] 
     { 
      new TabPage("tabPage 1"), 
      new TabPage("tabPage 2") 
     }); 

     tc.Location = new Point(20, 20); 
     tc.Size = new Size(300, 200); 
     this.ClientSize = new Size(350, 250); 
     this.Controls.Add(tc); 

     //renaming: 
     this.tc.TabPages[0].Text = "1st tab"; 
     this.tc.TabPages[1].Text = "2nd tab"; 

     //changing background: 
     this.tc.TabPages[0].BackColor = Color.Yellow; 
     this.tc.TabPages[1].BackColor = Color.YellowGreen; 

     //adding some controls to each tab: 
     TextBox tb = new TextBox(); 
     tb.Location = new Point(20, 20); 
     tb.Size = new Size(130, 20); 
     tb.Text = "This textBox is on 1st tab"; 

     Label lb = new Label(); 
     lb.Location = new Point(20, 20); 
     lb.Text = "This label is on 2nd tab"; 
     lb.ForeColor = Color.Red; 

     this.tc.TabPages[0].Controls.Add(tb); 
     this.tc.TabPages[1].Controls.Add(lb); 
    } 
} 
+0

嗨,我不知道爲什麼,tc.TabPages.AddRange()不工作,可能我必須做點什麼? – yozawiratama 2011-03-31 09:02:20

相關問題