Hi everyone, I'm really new to C#.net development, especially for win32 applications. I'm basically making a board game and was wondering if anyone could help me out with this predicament:
I have a dynamically created array based on the size of the Board (13x13 or 19x19). I can make the array fine and position the pictureBoxes over the background, but I want to be able to change the properties of all the pictureBoxes based on a Click event. Is there any way to define a universal click event for all the pictureboxes in the array that would only modify the clicked picturebox? I'm trying to make them visible and assigning an image path upon firing the click event.
Please help, my brain is totally fried.
Here's sample code:
public void createBoardArray(int numOfPieces)
{
Point gamePieceStart = new Point(3, 5);
Point loadingLoc = new Point(120, 140);
PictureBox loadingPic = new PictureBox();
gameBoard = new PictureBox[numOfPieces, numOfPieces];
//initializing array of images
for(int g = 0; g < numOfPieces; ++g)
{
for(int h = 0; h < numOfPieces; ++h)
{
gameBoard[g,h] = new PictureBox();
gameBoard[g, h].BackColor = System.Drawing.Color.Transparent;
gameBoard[g, h].InitialImage = null;
gameBoard[g, h].ErrorImage = null;
gameBoard[g, h].Width = 28;
gameBoard[g, h].Height = 26;
//adding click event
gameBoard[g, h].Click += new EventHandler(picBox_OnClick);
gameBoard[g, h].Location = gamePieceStart;
//test image assignment
// gameBoard[g, h].ImageLocation = Path.Combine(Environment.CurrentDirectory, @"bin\blackPiece3.gif");
gameBoard[g, h].Enabled = true;
gameBoard[g, h].Visible = false;
gamePanel.Controls.Add(gameBoard[g, h]);
gamePieceStart.Y += 31;
}
gamePieceStart.X += 31;
gamePieceStart.Y = 5;
}
}
I have a dynamically created array based on the size of the Board (13x13 or 19x19). I can make the array fine and position the pictureBoxes over the background, but I want to be able to change the properties of all the pictureBoxes based on a Click event. Is there any way to define a universal click event for all the pictureboxes in the array that would only modify the clicked picturebox? I'm trying to make them visible and assigning an image path upon firing the click event.
Please help, my brain is totally fried.
Here's sample code:
public void createBoardArray(int numOfPieces)
{
Point gamePieceStart = new Point(3, 5);
Point loadingLoc = new Point(120, 140);
PictureBox loadingPic = new PictureBox();
gameBoard = new PictureBox[numOfPieces, numOfPieces];
//initializing array of images
for(int g = 0; g < numOfPieces; ++g)
{
for(int h = 0; h < numOfPieces; ++h)
{
gameBoard[g,h] = new PictureBox();
gameBoard[g, h].BackColor = System.Drawing.Color.Transparent;
gameBoard[g, h].InitialImage = null;
gameBoard[g, h].ErrorImage = null;
gameBoard[g, h].Width = 28;
gameBoard[g, h].Height = 26;
//adding click event
gameBoard[g, h].Click += new EventHandler(picBox_OnClick);
gameBoard[g, h].Location = gamePieceStart;
//test image assignment
// gameBoard[g, h].ImageLocation = Path.Combine(Environment.CurrentDirectory, @"bin\blackPiece3.gif");
gameBoard[g, h].Enabled = true;
gameBoard[g, h].Visible = false;
gamePanel.Controls.Add(gameBoard[g, h]);
gamePieceStart.Y += 31;
}
gamePieceStart.X += 31;
gamePieceStart.Y = 5;
}
}