parameters in click or mouse down events

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
 
M

Morten Wennevik

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;
}
 
G

Guest

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.
 
G

Guest

Thank you so much, I was screwing up the syntax. Turns out the Tag is an
object and not a string also. Greatly appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top