C# 如何更改面板阵列中特定的颜色面板

C# 如何更改面板阵列中特定的颜色面板,c#,arrays,panel,C#,Arrays,Panel,我创建了一个带有面板的国际象棋棋盘,并为显示皇后和以下代码创建了8皇后问题。我无法在表单中更改show queen的颜色特定单元板 const int tileSize = 50; const int gridSize = 8; var clr1 = Color.Black; var clr2 = Color.White; // initialize the "chess board" _chess

我创建了一个带有面板的国际象棋棋盘,并为显示皇后和以下代码创建了8皇后问题。我无法在表单中更改show queen的颜色特定单元板

        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;

            }
const int tileSize=50;
常量int gridSize=8;
var clr1=颜色。黑色;
var clr2=颜色。白色;
//初始化“棋盘”
_棋盘面板=新面板[gridSize,gridSize];
//double for循环用于处理所有行和列
对于(var n=0;n
请帮助:我如何改变颜色面板的具体形式和显示?这个示例代码我wirte

                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;
                            }

                            ...
                    }
                }
int[]x=newint[]{1,5,6,7,3,0,2};
对于(int i=0;i<8;i++)
{
开关(一)
{
案例1:
{
newPanel.BackColor=Color.Red;
_棋盘格[x[i],i]=新棋盘格;
打破
}
...
}
}
参见下面的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        Panel[,] _chessBoardPanels = null;
        public Form1()
        {
            InitializeComponent();

            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;

                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                int[] x = new int[] { 1, 5, 6, 7, 3, 0, 2 };
                for (int i = 0; i < 8; i++)
                {

                    switch (i)
                    {
                        case 1:
                            {
                                _chessBoardPanels[x[i], i].BackColor  = Color.Red;
                                break;
                            }
                    }
                }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序13
{
公共部分类Form1:Form
{
Panel[,]u棋盘panels=null;
公共表格1()
{
初始化组件();
常数int tileSize=50;
常量int gridSize=8;
var clr1=颜色。黑色;
var clr2=颜色。白色;
//初始化“棋盘”
_棋盘面板=新面板[gridSize,gridSize];
//double for循环用于处理所有行和列
对于(var n=0;n
是否对案例1进行案例检查,但
i
已初始化为
0
,因此您知道数组是基于.net的
0
。。