yonan2236
yonan2236

Reputation: 13659

How to add a control to a Form programmatically?

I'm trying to add a control(Label) inside of a panel. Please see the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AddControlProgramatically
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Label lbl = new Label();

            for (int x = 0; x <= 3; x++)
            {
                //create new label location after each loop
                //by multiplying the new value of variable x by 5, so the new label 
                //control will not overlap each other.
                lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5));
                //create new id and text of the label
                lbl.Name = "label_" + x.ToString();
                lbl.Text = "Label " + x.ToString();

                this.panel1.Controls.Add(lbl);
            }
        }
    }
}

screenshot

Here's the form. What I'm trying to accomplish is to programatically generate 3 diffent control labels. But as you can see, It only displays the last one. Please help me regarding this. I know there's something wrong in my code (cause it's not working).Thanks...

Upvotes: 5

Views: 25690

Answers (4)

user3796218
user3796218

Reputation: 21

Very old question, but apparently nobody took the time to get it right.... You keep overwriting the same Label object instance. Instead create a list of Label instances and then add them to your form like this:

        List < Label > myLabels = new List<Label>();
        for (int i = 0; i < 5; i++)
        {
            Label lbl = new Label();                
            //create new id and text of the label
            lbl.Name = "label_" + i.ToString();
            lbl.Text = "Label " + i.ToString();
            lbl.Width = 50;
            lbl.Location = new System.Drawing.Point(52 + (i * lbl.Width), 50);
            myLabels.Add(lbl);
        }
        foreach (Label l in myLabels)
        {
            this.Controls.Add(l);
        }

Upvotes: 1

Anax
Anax

Reputation: 9382

You need to put Label lbl = new Label(); inside your for loop.

Upvotes: 0

Michael Buen
Michael Buen

Reputation: 39483

Put the Label lbl = new Label(); inside the loop.

And make the offset bigger, change this...

 lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5))

...to:

lbl.Location = new System.Drawing.Point(52 + (x * 30), 58 + (x * 30))

Upvotes: 6

Jerod Houghtelling
Jerod Houghtelling

Reputation: 4867

You need to create a new label in each loop iteration. Right now you are only creating one label.

private void button1_Click(object sender, EventArgs e)
{
    for (int x = 0; x <= 3; x++)
    {
        Label lbl = new Label();

        //create new label location after each loop
        //by multiplying the new value of variable x by 5, so the new label 
        //control will not overlap each other.
        lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5));
        //create new id and text of the label
        lbl.Name = "label_" + x.ToString();
        lbl.Text = "Label " + x.ToString();

        this.panel1.Controls.Add(lbl);
    }
}

Upvotes: 3

Related Questions