parameters in click or mouse down events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 68 picturebox objects in an array and I want to work with the
picturebox that is clicked on. How can I figure out which picturebox was
clicked on so that I can make one generic "click" routine instead of having
68 separate events like this?

private void pictureBox1_Click(object sender, System.EventArgs e)
{SelectedPicture = 0;}

private void pictureBox2_Click(object sender, System.EventArgs e)
{SelectedPicture = 1;}
..
..
..
etc
 
Hi dcurran,

This information is stored in the sender variable. Sender is always the
control that triggered the current run of the method.

private void pictureBox1_Click(object sender, System.EventArgs e)
{
if(sender == pictureBox1)
SelectedPicture = 0;
else if(sender == pictureBox2)
SelectedPicture = 1;
}

You might also use the Tag property to store useful information, for
instance a number

private void pictureBox1_Click(object sender, System.EventArgs e)
{
SelectedPicture = (int)((PictureBox)sender).Tag;
}
 
First you should bind the same event hanlder to all click events:

pictureBox1.Click += new EventHandler(pictureBox1_Click):
pictureBox2.Click += new EventHandler(pictureBox1_Click):
.....
pictureBox68.Click += new EventHandler(pictureBox1_Click):

and then do as Morten suggested.
 
Thank you so much, I was screwing up the syntax. Turns out the Tag is an
object and not a string also. Greatly appreciated
 
Back
Top