Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding an event to every member of pictureBox array

Status
Not open for further replies.

robusto33

Programmer
May 19, 2007
3
0
0
US
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;

}

}
 
just so that i'm clear ... right now as it stands whenever a picture is clicked all the pictures are changed because they are registered to the same callback method (pikBox_Click) correct?
 
Can you post your PictureBox code ... I have an idea that I know will work well for you but I don't like generalizing.
 
I want to have them all linked to a callback method that will change the properties of the item clicked on, even though its in an array of pictureBoxes. So if I click on gameBoard[2,3] I want the one method to only change the properties of the item it was called on. I'm just not sure how to do that using one method for the whole array; I'm sure its not too crazy, probably something small I've missed.

PictureBox is a built in C# control, I don't have any external code pertaining to the pictureBoxes other than the creation and positioning of the array.

Thanks for the help!
 
Ok your code will do exactly what you wanted. All PictureBoxs will call back to PicBox_Click ... thus being a "universal callback method". Easier approach to modifying the appropriate box is just typecast the sender object. You already know that it can only be a PictureBox so there is no chance for runtime exceptions. Now you have an instance of the clicked PictureBox so that you can modify it. Hope that works for ya.

I'm just throwin this idea out there based on the assumption that the sender object will be the PictureBox and not the Panel that contains them.

Example:
Code:
private void PicBox_Click(object sender, EventArgs arg)
{
   PictureBox box = (PictureBox)sender;
   //box. do whatever
}
 
Thanks so much for your help. I knew it was something easy, I just didn't understand how the sender works, but I thought it might have something to do with that. wewt
 
robusto33, the solution provided by mcox05, although simple, has surely solved a big problem. As you are new to Tek-Tips, I thought that I would draw your attention to the link at ther bottom left of every reply. Clicking one of those links below one of his replies would enable you to thank him by giving him a star
star.gif


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top