Hi guys,

I am trying to write a code which will create labels at runtime vertically after selecting a value from a combobox. I am only getting one label created at the starting position.
Can you please tell me what the problem is and how can I fix it? Thanks

Here is my code:

private Label[] label; 

int b = 70, d = 157;  //these two are the coordinates where the label(s) should start creating 

for (int i = 1; i < count; i++)
            {
                label[i] = new Label();
                label[i].Visible = true;
                label[i].Location = new System.Drawing.Point(b, d);
                label[i].Size = new Size(80, 70);
                label[i].TabIndex = 1;
                label[i].Text = i.ToString();
               
                this.Controls.Add(label[i]);
                
                this.ResumeLayout(false);
                d += 40;

                
                
            }

Recommended Answers

All 5 Replies

private Label[] label=new Label[5]; 

int b = 70, d = 157;  //these two are the coordinates where the label(s) should start creating 

for (int i = 1; i <label.Length; i++)
            {
                label[i] = new Label();
                label[i].Visible = true;
                label[i].Location = new System.Drawing.Point(b, d);
                label[i].Size = new Size(80, 70);
                label[i].TabIndex = 1;
                label[i].Text = i.ToString();
               
                this.Controls.Add(label[i]);
                
                this.ResumeLayout(false);
                d += 40;

                
                
            }

Thanks for the quick reply! I tried it but it doesn't work. It still creates only first label at the given position.

Here is the code

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

            
               private Label[] l;


       private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

          
            int count = int.Parse( comBoxNumOfCourses.SelectedItem.ToString());
            
            int b=157, d=75; 
            
            l = new Label[count];

            for (int i = 1; i < l.Length; i++)
            {
                l[i] = new Label();
                l[i].Visible = true;
                l[i].Location = new System.Drawing.Point(b, d);
                l[i].Size = new Size(80, 70);
                l[i].TabIndex = 1;
                l[i].Text = i.ToString();

                this.Controls.Add(l[i]);

                this.ResumeLayout(false);
                d += 40;
            }

I hope this will help: (i use text boxes instead)

int x, y;
            x = 10; y = 20;
 for (int i = 1; i < l.Length; i++)
            {
               TextBox tx = new TextBox();
                tx.Size = new Size(80, 10);
                tx.Text = o.ToString();
                tx.TextAlign = HorizontalAlignment.Center;
                tx.Location = new Point(x, y);
                tx.BackColor = Color.SkyBlue;
                this.Controls.Add(tx);
                        y = y + 40;
}

But I have a further problem.
After creating the labels, how we invoke events on them?

thxs @sandeepani its right code

but i use like this

int yLoc = 30;

            for (int i = 0; i < 10; i++)
            {
                Label l1 = new Label();
                l1.Name = "label" + i.ToString();
                l1.Text = "love islam";
                
                l1.Location = new Point(31,yLoc);

                this.Controls.Add(l1);
                yLoc += 20;
            }

For Asp Runtime Labels code like this:-

for (int i = 0; i < 10; i++)
          {
            Label l1 = new Label();
            l1.Text = "love islam";
            this.Controls.Add(l1);

            this.Controls.Add(new LiteralControl("<br>"));
           }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.