【发布时间】:2016-10-21 15:45:48
【问题描述】:
我创建了一个带有面板的棋盘,并为显示皇后和以下代码创建了 8 个皇后问题。我无法在表单中更改表演女王的颜色特定单元格板。
const int tileSize = 50;
const int gridSize = 8;
var clr1 = Color.Black;
var clr2 = Color.White;
// initialize the "chess board"
_chessBoardPanels = new Panel[gridSize, gridSize];
// double for loop to handle all rows and columns
for (var n = 0; n < gridSize; n++)
{
for (var m = 0; m < gridSize; m++)
{
// create new Panel control which will be one
// chess board tile
var newPanel = new Panel
{
Size = new Size(tileSize, tileSize),
Location = new Point(tileSize * n, tileSize * m)
};
// add to Form's Controls so that they show up
Controls.Add(newPanel);
// add to our 2d array of panels for future use
_chessBoardPanels[n, m] = newPanel;
// color the backgrounds
if (n % 2 == 0)
newPanel.BackColor = m % 2 != 0 ? clr1 : clr2;
else
newPanel.BackColor = m % 2 != 0 ? clr2 : clr1;
}
请帮助:我如何更改特定的颜色面板并以形式显示?这个示例代码是我写的。
int[] x = new int[] { 1, 5, 6, 7, 3, 0, 2 };
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 1:
{
newPanel.BackColor = Color.Red;
_chessBoardPanels[x[i], i] = newPanel;
break;
}
...
}
}
【问题讨论】:
-
您是否对案例 1 进行案例检查,但
i已初始化为0,因此您知道数组是基于 .net 的0。