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

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

2

4 Answers

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

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

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

0

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

Your Answer

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

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest