简体   繁体   中英

Problem with adding controls into panel in WinForm

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?

I can see that you already have an instance of tmpRedSquare, tmpBlueSquare, tmpGreenSquare and tmpYellowSquare, and it seems that you are expecting those instances to appear multiple times in your panel. Well, that's not how it works, you need to create each time a new instance of the expected Button to be added and provide that to the pnlChessBoard.Controls.Add() method.

I recommend you to create methods responsible for creating a new instance of those buttons (like CreateRedSquare() , CreateBlueSquare() , etc.) when they are called, and then use the returned instance instead:

if (t == 0)
{
      Button RedSquare = CreateRedSquare();
      square[t, i] = RedSquare;
}
...

Also, you will have to rethink your logic around the Location of the button when it's added to the panel.

You have just using 4 buttons. You need to create new Button() . Here is my sample code. You may need to calculate back X and Y according to your design. Hope it works.

public Button CreateBtnRedSqure()
{
    Button b = new Button();
    b.BackColor = Color.Red;
    .....
    .....
    return b;
}

    int Y = 0;
    for (int i = 0; i < 16; i++)
    {
        int X = 0;
        for (int t = 0; t < 4; t++)
        {
            if (t == 0)
            {
                Button RedSquare = CreateBtnRedSqure();
                RedSquare.Location = new Point(X, Y + Constant.SquareMiddleSpace);
                square[t, i] = RedSquare;
            }
            else if (t == 1)
            {
                Button BlueSquare = CreateBtnBlueSqure();
                BlueSquare = new Point(X, Y + Constant.SquareMiddleSpace);
                square[t, i] = BlueSquare;
            }
            else if (t == 2)
            {
                Button GreenSquare = CreateBtnGreenSqure();
                GreenSquare = new Point(X, Y + Constant.SquareMiddleSpace);
                square[t, i] = GreenSquare;
            }
            else if (t == 3)
            {
                Button YellowSquare = CreateBtnYellowSqure();
                YellowSquare = new Point(X, Y + Constant.SquareMiddleSpace);
                square[t, i] = YellowSquare;
            }
            pnlChessBoard.Controls.Add(square[t, i]);
            X = X + (*width of your btn size)
        }
            Y = Y + (* height of your btn size );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM