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.
Your
yvariable is defined in the for loop, not outside. Therefore, for each execution of the loop, you initialize it to10and use it in yourSystem.Drawing.Point. If you want to keep track of the increment done at the end of the loop, you must declare and initializeybefore the for loop.