0

我需要为游戏制作一个 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;
4

1 回答 1

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]);
        }
于 2013-03-18T14:43:25.130 回答