从数组创建标签 [英] 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;
}
}
调试
Debug.WriteLine(hardrive.GetHardDriveName.Count);
在数组中显示两个项目.
Shows two items in the array.
问题在于,GroupBox中只有一个标签,而不是两个.
The problem is that in the GroupBox there is only one label instead of two.
推荐答案
您的y
变量是在for循环中定义的,而不是在外部定义的.因此,对于循环的每次执行,都将其初始化为10
并在System.Drawing.Point
中使用它.如果要跟踪循环结束时完成的增量,则必须在for循环之前声明并初始化y
.
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;
}
这篇关于从数组创建标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文