which picture box was clicked?

  • Thread starter Thread starter John McD
  • Start date Start date
J

John McD

Hi,

I have 64 pictureboxes that all use the same MouseDown
function. How can I detect which picturebox has been
clicked upon? Can I add an extra parameter to the Event
function?

TIA...John.

gridCell[count].MouseDown += new
MouseEventHandler(gridCell_MouseDown);
...
...
...
private void gridCell_MouseDown(object sender,
MouseEventArgs e)
{
this.textBox1.Text = sender.ToString();
}
 
An easy (although innefficient way) is to iterate through the controls and
try a tit test based on the clicked point. This might work (although I
haven't tested):

Rectangle rect = new Rectangle(0, 0, 1, 1);

PictureBox pictureBoxHit = null;

foreach (PictureBox pictureBox in pictureBoxHashTable.Values)

{

rect.X = e.X;

rect.Y = e.Y;

if (pictureBox.Bounds.IntersectsWith(rect))

{

pictureBoxHit = pictureBox;

break;

}

}

if (pictureBoxHit != null)

{

// Hit code goes here

}
 
In the initialization of the gridCell pictureBoxes
you can initialise the unique name to the picture box

if count is unique,

gridCell[count].Name=count.ToString();
gridCell[count].MouseDown += new
MouseEventHandler(gridCell_MouseDown);

private void gridCell_MouseDown(object sender,
MouseEventArgs e)
{
PictureBox pb=(PictureBox)sender; MessageBox.Show(pb.Name);

-----Original Message-----
Hi,

I have 64 pictureboxes that all use the same MouseDown
function. How can I detect which picturebox has been
clicked upon? Can I add an extra parameter to the Event
function?

TIA...John.

gridCell[count].MouseDown += new
MouseEventHandler(gridCell_MouseDown);
...
...
...
private void gridCell_MouseDown(object sender,
MouseEventArgs e)
{
this.textBox1.Text = sender.ToString();
}
.
 
Back
Top