2013-03-18 58 views
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; 
+0

?會發生什麼,你期待什麼? – 2013-03-18 14:33:35

+0

定義'沒有似乎工作'。你是否收到錯誤消息或意外行爲或其他內容?另外,在哪裏定義和設置了「列」? – 2013-03-18 14:34:36

+1

設置可視性怎麼樣?什麼不起作用?你在做什麼?什麼技術? – 2013-03-18 14:34:38

回答

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]); 
     } 
+0

非常感謝您的先生! 我沒有注意到this.controls! – Kapein 2013-03-18 14:46:06