Achieve select and deselect with a change of color on an array of panels

C# Amateur 1 Reputation point
2021-11-13T07:57:20.773+00:00

I am trying to achieve a click effect on an array of panels such that when PanelA is clicked then a blue background is applied to it, when another panelB is clicked, panelA is decolorized back to it's original color and then blue background is applied to the new Panel that was clicked. The panels are the squares in a checkerboard game an building, here is my code

public class Checkerpiece
    { 
        //declare a panel array for storing clicked Panels
        List<Panel> clicked_squares=new List<Panel>();
        //The object above can only hold two panel elements
        //colors of the rounded pieces
        Color color;
        //specify where the checker is drawn
        Panel target_square;
        //specify the center of the circle
        float center_x;
        float center_y;
        //specify the radius of the checker piece
        float radii;
        //define some foreground color that will be used to access the color of the checker
        Color foreground;
        //fill the details inside the constructor
        public Checkerpiece(Panel mypanel,Color color)
        {
            this.color = color;
            this.target_square = mypanel;
            this.center_x = mypanel.Width / 2;
            this.center_y = mypanel.Height / 2;
            this.radii = mypanel.Width / 2;
            //register an onclick listener for our checkerpiece object
            this.target_square.Click += Target_square_Click;
           // this.target_square.MouseClick += Target_square_MouseClick;
        }


        private void Target_square_Click(object sender, EventArgs e)
        {
            clicked_squares.Add(target_square);
           //this keeps showing 1 no mater what panel I click
            MessageBox.Show(clicked_squares.Count.ToString());
         }
          public static void fillCircle(Graphics g, Brush b, float centerX, float centerY, float radius)
        {
            g.FillEllipse(b, centerX - radius, centerY - radius,
                     radius + radius, radius + radius);
        }
        //implement a getter for the color used to used to draw the checker
        public Color getCheckerColor()
        {
            return this.color;
        }
  }

public partial class Form1 : Form
{
private Panel[,] _chessBoardPanels;
public Form1()
{
InitializeComponent();

    }


    private void Form1_Load(object sender, EventArgs e)
    {
        const int tileSize = 100;
        const int gridSize = 8;
        var clr1 = Color.DarkGray;
        var clr2 = Color.White;

        // initialize the "chess board"
        _chessBoardPanels = new Panel[gridSize, gridSize];
        int count = 0;
        // 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)

            };
                //register an event listener for each square


                // 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;
                //draw only the red checkers on top of the board

                // color the backgrounds
                if (n % 2 ==0)
                    newPanel.BackColor = m % 2 != 0 ? clr1 : clr2;
                    //new Checkerpiece(newPanel, Color.Red).draw();

                else
                    newPanel.BackColor = m % 2 != 0 ? clr2 : clr1;
                //draw a new checker piece
                if (newPanel.BackColor==Color.White &&m<=3)
                    new Checkerpiece(newPanel, Color.Red).draw(); 
                   ;
                if (newPanel.BackColor == Color.White && m > 4)
                    new Checkerpiece(newPanel, Color.Black).draw();

            }
        }

    }

}
Output

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,984 questions
{count} votes