Linked Questions

Popular Questions

Problem with adding controls into panel in WinForm

Asked by At

I'm creating a chessboard for my game. I have 64 buttons, for some reason I just can add 4 buttons into the panel. This is my code

for (int i = 0; i < 16; i++)
        {
            for (int t = 0; t < 4; t++)
            {
                if (t == 0)
                {
                    Button RedSquare = tmpRedSquare;
                    square[t, i] = RedSquare;
                }
                else if (t == 1)
                {
                    Button BlueSquare = tmpBlueSquare;
                    square[t, i] = BlueSquare;
                }
                else if (t == 2)
                {
                    Button GreenSquare = tmpGreenSquare;
                    square[t, i] = GreenSquare;
                }
                else if (t == 3)
                {
                    Button YellowSquare = tmpYellowSquare;
                    square[t, i] = YellowSquare;
                }
                pnlChessBoard.Controls.Add(square[t, i]);
            }
            tmpRedSquare.Location = new Point(tmpRedSquare.Location.X, tmpRedSquare.Location.Y + Constant.SquareMiddleSpace);
            tmpBlueSquare.Location = new Point(tmpBlueSquare.Location.X + Constant.SquareMiddleSpace, tmpBlueSquare.Location.Y);
            tmpGreenSquare.Location = new Point(tmpBlueSquare.Location.X, tmpBlueSquare.Location.Y - Constant.SquareMiddleSpace);
            tmpYellowSquare.Location = new Point(tmpYellowSquare.Location.X - Constant.SquareMiddleSpace, tmpYellowSquare.Location.Y);
        }

I expected the result should be 64 buttons on the form. The result is I just see 4 buttons (first 4 buttons when executing), I have debugged and realize that the panel just contains 4 controls after the program executed.
The code always go through the command, and it also the problem:

pnlChessBoard.Controls.Add(square[t, i]);

I didn't know how the panel didn't add other 62 buttons. So what is the main problem?

Related Questions