tags:

views:

36

answers:

3

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

alt text

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

+1  A: 

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);
    }
}
Jerod Houghtelling
I just did what you said, but there's no change at all...
yonan2236
It's not working sir..
yonan2236
Hmmm, looking at the code it looks like 4 labels should be created. (`x <= 3`).
Jerod Houghtelling
the first label only is being added when i run the program...
yonan2236
I wish I'm good as you in programming. I only started programming last year because of my friend. I'm not an IT person, but i find it very fun and interesting. Now I'm doing self study, and I choose C# language to study. My friend also lend me books about programming. (I think I want to shift my course)... : )
yonan2236
Sir it's already working... its just the spacing that makes the other labels not to appear, they overlapped each other.... I really appreciate your responses. Thanks again.
yonan2236
@yonan2236 Looking back the spacing does seem like it wasn't enough, sorry I missed that! Hang in there it gets easier the more familiar you get with a programming language!
Jerod Houghtelling
A: 

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

Anax
ok thanks...... :)
yonan2236
I just did what you said, but there's no change at all...
yonan2236
It's working... thanks sir
yonan2236
A: 

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))
Michael Buen
@yonan2236: this is your chance to increase your accept rate. accept this answer ;-)
Michael Buen
now it's working : )thank you sir...
yonan2236
much welcome :-)
Michael Buen