简体   繁体   中英

use a picture box on a panel

I need some help. I'm working on a chess game. I created my chess board on a panel, with pictureBox. As the code below

for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {

                    PictureBox boardcase = new PictureBox();
                    boardcase.Size = new Size(100, 100);
                    //if (i == j) { boardcase.BackColor = Color.White; }
                    if ((i + j) % 2 == 0) { boardcase.BackColor = Color.DarkBlue; }
                    else { boardcase.BackColor = Color.Gray; }
                    boardcase.Location = new Point(i * (boardcase.Width + 5) + 45, j * (boardcase.Height + 5) + 15);
                    panel1.Controls.Add(boardcase);

                }

            } 

Then, I would like to know how I can use individually each pictureBox that i created, outside this block. thanks a lot

Haha, ok.I used @TaW 's solution in comment section. and this is it… I just created a PictureBox List as: List<PictureBox> plateau = new List<PictureBox>(); and assigned each boardcase to the list in my For Loop. plateau.Add(boardcase); Then I can call plateau[i] in my code. :-) Thanks guys.

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