473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

parameters in click or mouse down events

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

Nov 16 '05 #1
3 2646
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;
}
On Sun, 6 Feb 2005 21:59:03 -0800, dcurran
<dc*****@discussions.microsoft.com> wrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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.

"dcurran" wrote:
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

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

"Morten Wennevik" wrote:
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;
}
On Sun, 6 Feb 2005 21:59:03 -0800, dcurran
<dc*****@discussions.microsoft.com> wrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BadOmen | last post by:
I want my program to send a mouse click to the window at the current mouse position, how do I do that? Example: I have my mouse over a button in Word and then my program is sending the left...
5
by: Don | last post by:
Is there a way to capture click events in the form designer? I'm trying to create a control similar to the TabControl (which seems to handle clicks in design mode) and would like to be able to...
5
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event...
3
by: Morten Snedker | last post by:
If I have a number of random applications open, move the mouse cursor to a given position and do a click, the application gets the focus. That is what this simple code should illustrate: Dim...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.