Add Dynamiclly controls to a Form

Hi you experts.

I have a for loop and i want every time the code enters the loop to be able to add a label and a textbox into my form.The label i want it to be on the right of the textBox. Every group (label-textbox) that is added must be under the previous group.

Can you give me an example?
I can't find anything on the web that can help me.

Thank you all in advance.

LVL 1
adktdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ssdjgru1Commented:
Hello adktd,

the form has containter for other controls that it displays. it is accessed through for example Form1.Controls. if you want to add Textbox the code should be:

TextBox txtBox = new TextBox; //Dinamicaly created text box
Form1.Controls.Add(txtBox); //Adding textbox to your form

If you need more specific help just write me ;)
0
adktdAuthor Commented:
Can you give me an example with a for loop?
And create the controls as i specified in my first post?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ssdjgru1Commented:
Here you go:

            for (int i = 0; i < 10; i++)
            {
                TextBox txtBox = new TextBox();
                txtBox.Location = new Point(10, txtBox.Height * i * 2);
               
                Label lblBox = new Label();
                lblBox.Text = "Label " + i;
                lblBox.Location = new Point(txtBox.Width + 10, txtBox.Height * i * 2);
               
                this.Controls.Add(lblBox);
                this.Controls.Add(txtBox);

            }

it will display 10 labels with 10 textboxes!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adktdAuthor Commented:
Cool.It works fine.
If i want every pair(label-textbox) to be in a group box how the above code will be?
0
ssdjgru1Commented:
               GroupBox grpBox = new GroupBox();
                grpBox.Size = new Size(200, 40);
                grpBox.Location = new Point(10, i * 40);
               
                TextBox txtBox = new TextBox();
                txtBox.Location = new Point(10, 15);
               
                Label lblBox = new Label();
                lblBox.Text = "Label " + i;
                lblBox.Location = new Point(txtBox.Width + 10, 15);
               
                grpBox.Controls.Add(lblBox);
                grpBox.Controls.Add(txtBox);

                this.Controls.Add(grpBox);
0
ssdjgru1Commented:
ups, I missed the for statement:

            for (int i = 0; i < 10; i++)
            {
                GroupBox grpBox = new GroupBox();
                grpBox.Size = new Size(200, 40);
                grpBox.Location = new Point(10, i * 40);
               
                TextBox txtBox = new TextBox();
                txtBox.Location = new Point(10, 15);
               
                Label lblBox = new Label();
                lblBox.Text = "Label " + i;
                lblBox.Location = new Point(txtBox.Width + 10, 15);
               
                grpBox.Controls.Add(lblBox);
                grpBox.Controls.Add(txtBox);

                this.Controls.Add(grpBox);
            }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.