我想寫一些代碼,顯示面板(網格[])的網格到更大的面板(gridHolder)。這裏是我到目前爲止的代碼:C#:製作面板的二維陣列
public void setupPanels(int x, int y)
{
grid = new Panel[y, x];
this.Controls.Add(gridHolder);
gridHolder.Show();
gridHolder.Location = new Point(0 , 0);
gridHolder.Size = new Size(x * PANEL_SIZE, y * PANEL_SIZE);
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
gridHolder.Controls.Add(grid[i, j]);
grid[i, j].Location = new Point(i * PANEL_SIZE, j * PANEL_SIZE);
gridHolder.Size = new Size(PANEL_SIZE, PANEL_SIZE);
}
}
}
當我嘗試運行該程序,我得到一個調試錯誤說「的NullReferenceException是未處理」。我如何修復我的代碼?
這將是有益的知道在哪一行,你得到「的NullReferenceException」。 – 2015-03-31 15:07:34