2

hi everybody i want to ask : How to add a tab programmically.

for my problem i have a tab control, as default only one tab. and i have a button when i click that button will add one other tab. so will be two tab.

please help me using c# and xaml.

1
  • 2
    I'd also like to say that like many of us I enjoy sharing my knowledge, helping people and getting helped, but this is not my job, so I'd encourage people asking questions to google a little bit before... TabControl.Items.Add is one of the first things you get to learn if you type something like "TabControl WPF tutorial" with El goog... Commented Mar 30, 2011 at 14:18

3 Answers 3

6
tabControl.Items.Add(yourNewTabItem);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this way:

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

Comments

0

Some more code to create and modify tabPages manually:

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);
    }
}

1 Comment

hi,i dont know why, tc.TabPages.AddRange() not work, may be i must do something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.