繁体   English   中英

以编程方式添加标签,C#标签控件

[英]add a tab programically, c# tab control

大家好,我想问一下:如何以编程方式添加标签。

对于我的问题,我有一个选项卡控件,默认情况下只有一个选项卡。 当我单击该按钮时,会有一个按钮将添加另一个选项卡。 因此将是两个选项卡。

请帮助我使用C#和XAML。

tabControl.Items.Add(yourNewTabItem);

尝试这种方式:

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

其他一些用于手动创建和修改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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM