Brandon
Brandon

Reputation: 161

How to add a control to a added tab C#

I am creating a simple application that keeps track of coins. I have a button that creates a new tab in a tabcontrol container. I am wanting to add some textboxes to the newly added tabs. The below code adds a textbox to my main tab called "Control". I have tried playing around with that field, but it always adds it to the main page called control. How would I do this? I have the following code:

           string name = txtName.Text;


            //validate information
            try { }
            catch { }

            //create new tab
            string title = name;
            TabPage myTabPage = new TabPage(title);
            tabControl1.TabPages.Add(myTabPage);


            //Add text boxes

                TextBox tb = new TextBox();
                tb.Location = new System.Drawing.Point(250, 75);
                tb.Name = "TextBoxName";
                tb.Size = new System.Drawing.Size(184, 20);
                Control.Controls.Add(tb);

            //put data inside of textboxes

                tb.Text = txtCoin.Text;

Upvotes: 0

Views: 1215

Answers (3)

fardjad
fardjad

Reputation: 20394

change

Control.Controls.Add(tb)

to

tabControl1.TabPages.Last().Controls.Add(tb)

tabControl1.TabPages.OfType<TabPage>().Last().Controls.Add(tb)

Upvotes: 1

James Hill
James Hill

Reputation: 61793

You need to add the text box to the controls collection of the new tab:

//Control.Controls.Add(tb); //Replace this line
myTabPage.Controls.Add(tb);

Upvotes: 2

SLaks
SLaks

Reputation: 887225

myTabPage.Controls.Add(tb);

Upvotes: 3

Related Questions