• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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.

0
adktd
Asked:
adktd
  • 4
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now