如何在面板数组中更改特定的面板颜色?

本文关键字:颜色 数组 | 更新日期: 2023-09-27 18:14:18

我创建了一个带有面板的国际象棋棋盘,我为显示皇后和以下代码创建了8皇后问题。我不能改变特定颜色的cell board for show queen in form。

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

如何在面板数组中更改特定的面板颜色?

见下面的代码:

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