Tom
Tom

Reputation: 245

How do I add a PictureBox chess piece over the top of a PictureBox chess board so that the position is relative to the chess board?

How do I add a PictureBox chess piece over the top of a PictureBox chess board so that the position is relative to the chess board?

PictureBox chessPiece = new PictureBox();
chessPiece.Image = Properties.Resources.PawnBlack;
chessPiece.BackColor = Color.Transparent;
chessPiece.BringToFront();
chessBoard.Controls.Add(chessPiece);
chessPiece.Location = new Point(0, 0);

Can a picture box be added to another picture box or do I need a 3rd element to house both the board and then the pieces?

To be more clear, The picture box/s will show up at the point (0,0) relative to the form not the chess board.

Upvotes: 0

Views: 385

Answers (1)

Bhaskar
Bhaskar

Reputation: 1036

You have to add chess piece location relative to the chess board location, so

chessPiece.Location = new Point(chessBoard.Location.X + x, chessBoard.Location.Y + y);

// x,y is chesspiece location relative to the chessboard

PS: Try using tablelayoutpanel instead of picturebox for the chessboard.

Upvotes: 1

Related Questions