C# 从数组创建标签

C# 从数组创建标签,c#,winforms,arrays,label,C#,Winforms,Arrays,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.GetHardDri

我想基于数组创建标签,但我总是只得到一个标签

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;
    }
}
显示数组中的两项


问题在于,在GroupBox中只有一个标签而不是两个。

在循环的每次迭代开始时,您将y重置回10

将x和y的声明移到循环之外

这:

for(int i=0;i
应该是:

int x = 10;
int y = 10;
for (int i = 0; i < hardrive.GetHardDriveName.Count; i++)
{    
    ....
intx=10;
int y=10;
对于(int i=0;i
不,您正在创建所有正确的标签-但是它们都具有相同的位置(10,10)。如果您想看到多个标签,您需要将它们放在不同的位置:)或者在循环外声明
y
,或者简单地使用:

lblHDDName[i].Location = new Point(10, i * 10 + 10);
与硬编码位置不同,您可能希望查看一些描述的自动排列控件

此外,看起来您根本不需要标签数组-您以后不会使用它们。例如,您可以:

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);
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
对于(int i=0;i
甚至:

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"
        });
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
对于(int i=0;i
您的
y
变量是在for循环中定义的,而不是在for循环外部。因此,对于每次循环执行,您都将其初始化为
10
,并在
系统.Drawing.Point
中使用它。如果您想跟踪循环结束时完成的增量,必须在for l之前声明并初始化
y
哎呀

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

   // increment it
   y += 10;
}
inty=10;
对于(int i=0;i<…;i++)
{
//使用y
...
//增加它
y+=10;
}
移动

int x = 10;
int y = 10;

超出for循环

您遇到的一个问题是
x
y
声明位于循环内部,因此
y
将始终为10,即使在循环末尾向其添加10。标签将始终处于相同的位置。若要在for循环外部修复此移动
int y=10
,您应该d移动
intx=10
也移动到那里。

移动

int x=10;
int y=10;
圈外

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;
    }
将y增加30

如果(您希望标签与其起点对齐)

int x=10;
可以保留在循环中

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;
    }
intx=10;
对于(int i=0;i
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;
    }