我需要为游戏制作一个6x6网格,但没有一个标签可以正常工作,
它可能很小。
Label[][] map = new Label[6][];
for (int i = 0;i < columns;i++)
{
map[i] = new Label[6];
for (int j = 0; j < columns; j++)
{
map[i][j] = new Label();
map[i][j].AutoSize = true;
map[i][j].BackColor = Color.Black;
map[i][j].Location = new Point(i * spacing, j * spacing);
map[i][j].Name = "map" + i.ToString() + "," + j.ToString();
map[i][j].Width = spacing;
map[i][j].Height = spacing;
map[i][j].TabIndex = 0;
map[i][j].Text = "test" + i.ToString() + j.ToString();
panel1.Controls.Add(map[i][j]);
}
this.Controls.AddRange(map[i]);
}
MessageBox.Show("greatsuscces");
return;
答案 0 :(得分:1)
确保您的Panel
足够大,首先可能是500x500。其次,您需要将所有标签添加到Panel
而不是this.Controls
int spacing = 75;
int columns = 6;
//Use your variable above to create the array
Label[][] map = new Label[columns][];
for (int i = 0; i < columns; i++) {
//Create a new sub array
map[i] = new Label[columns];
for (int j = 0; j < columns; j++) {
map[i][j] = new Label();
map[i][j].AutoSize = true;
map[i][j].BackColor = Color.Black;
map[i][j].Location = new Point(i * spacing, j * spacing);
map[i][j].Name = "map" + i.ToString() + "," + j.ToString();
map[i][j].Width = spacing;
map[i][j].Height = spacing;
map[i][j].TabIndex = 0;
map[i][j].Text = "test" + i.ToString() + j.ToString();
}
//Add the range to the panel
panel1.Controls.AddRange(map[i]);
}