Chuck
Chuck

Reputation: 139

Create labels from array

I want to create labels based on an array, but i always get only one label.

private void button1_Click(object sender, EventArgs e)
{
    Debug.WriteLine(hardrive.GetHardDriveName.Count);
    Label[] lblHDDName = new Label[hardrive.GetHardDriveName.Count];

    for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
    {
        int x = 10;
        int y = 10;

        lblHDDName[i] = new Label();
        lblHDDName[i].Location = new System.Drawing.Point(x, y);
        lblHDDName[i].Text = "Test";
        groupBoxHDD.Controls.Add(lblHDDName[i]);

        y += 10;
    }
}

Debugging

Debug.WriteLine(hardrive.GetHardDriveName.Count);

Shows two items in the array.

The problem is that in the GroupBox there is only one label instead of two.

Upvotes: 0

Views: 3810

Answers (6)

Metod Medja
Metod Medja

Reputation: 768

One of the problems you have is that the x and y declarations are inside the loop so y will always be 10 even though you add 10 to it in the end of the loop. The labels will always be in the same position. To fix this move int y = 10 outside of the for loop. You should move int x = 10 there as well.

Upvotes: 1

Ankush Roy
Ankush Roy

Reputation: 1641

move

int x=10;
int y=10;

outside the loop

and increment y by 30

if( you want the labels to align themselves to their starting point

int x=10;

can remain inside the loop

int x = 10;
for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
    {

        int y = 10;

        lblHDDName[i] = new Label();
        lblHDDName[i].Location = new System.Drawing.Point(x, y);
        lblHDDName[i].Text = "Test";
        groupBoxHDD.Controls.Add(lblHDDName[i]);

        y += 30;
    }

Upvotes: 0

Peter
Peter

Reputation: 38555

move the

int x = 10;
int y = 10;

out of the for loop

Upvotes: 1

Nekresh
Nekresh

Reputation: 2988

Your y variable is defined in the for loop, not outside. Therefore, for each execution of the loop, you initialize it to 10 and use it in your System.Drawing.Point. If you want to keep track of the increment done at the end of the loop, you must declare and initialize y before the for loop.

int y = 10;
for (int i = 0; i < ...; i++)
{
   // use y
   ...

   // increment it
   y += 10;
}

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1503449

Nope, you're creating all the right labels - but they've all got the same position of (10, 10). If you want to see more than one of them, you'll need to put them in difference places :) Either declare y outside the loop, or simply use:

lblHDDName[i].Location = new Point(10, i * 10 + 10);

Rather than hard-coding the positions, you may want to look into an auto-arranging control of some description.

Also, it looks like you don't really need the array of labels at all - you're not using them afterwards. For example, you could have:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
    {
        // Assuming you're using C# 3 or higher
        Label label = new Label {
            Location = new Point(10, i * 10 + 1),
            Text = "test"
        };
        groupBoxHDD.Controls.Add(label);
    }
}

or even:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
    {
        groupBoxHDD.Controls.Add(new Label {
            Location = new Point(10, i * 10 + 1),
            Text = "test"
        });
    }
}

Upvotes: 1

Jamiec
Jamiec

Reputation: 136174

You're resetting y back to 10 at the start of each iteration of the loop.

move the declaration of x and y outside of the loop.

this:

for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
{
    int x = 10;
    int y = 10;
    ....

should be:

int x = 10;
int y = 10;
for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
{    
    ....

Upvotes: 1

Related Questions