Create labels from array

删除回忆录丶 提交于 2019-12-25 03:29:21

问题


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.


回答1:


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



回答2:


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++)
{    
    ....



回答3:


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



回答4:


move the

int x = 10;
int y = 10;

out of the for loop




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/5004287/create-labels-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!