Windows Develop Bookmark and Share   
 index > Windows Forms General > Trying to tell which item set off an event
 

Trying to tell which item set off an event

(Sorry if this is easy or impossible. I'm still reasonably new to C# and can't find this anywhere.)

I have created a program containing something similiar to:

PictureBox[] Images = new PictureBox[Total];

Images[0] = new PictureBox();

Images[0].Click += new EventHandler(Clicked_Click);

The thing is that allImages created this wayuse the same EventHandler, as I can't give them all seperate ones because the value of thevariable Total might change at some point. I want all Images to use the same EventHandler, but I only want the Image clicked upon to be affected when the click happens.

So my question is: Is there a way to tell WHICH image was clicked on, or is there a better way to do this?

The value of sender is "System.Windows.Forms.PictureBox, SizeMode: StretchImage" and e returns "System.Windows.Forms.MouseEventArgs", within the EventHandler.

Thanks.

gamer6000  Monday, January 22, 2007 12:33 PM
You can cast the sender into PictureBox:

private void Clicked_Click(object sender, EventArgs e)
{
PictureBox picture = sender as PictureBox;
if (picture == null)
return;

//do something on picture clicked using picture variable
}
boban.s  Monday, January 22, 2007 12:50 PM
You can cast the sender into PictureBox:

private void Clicked_Click(object sender, EventArgs e)
{
PictureBox picture = sender as PictureBox;
if (picture == null)
return;

//do something on picture clicked using picture variable
}
boban.s  Monday, January 22, 2007 12:50 PM

Hi,

sender parameter always points to the object, which raised the event. In the Clicked_Click handler, try casting it to PictureBox:

private void Clicked_Click(object sender, EventArgs e)
{
   
PictureBox pictureClicked = sender as PictureBox;
   
if (pictureClicked == null)
   
{
       
//Not a picture box...
       
return;
   
}
   
pictureClicked.Image.Save(filename);
}

Andrej

Andrej Tozon  Monday, January 22, 2007 12:51 PM

Thanks for the help and quick replies. It helped a lot.

Unfortunately, I am still left with a bit of a problem. I can affect the clicked upon image, but I can't tell which number of the array it is. Is there anyway to use the variable picture from the"PictureBox picture = sender as PictureBox" thing to know which number of the array of Images was clicked on?

Thanks.

gamer6000  Monday, January 22, 2007 1:32 PM
When creating picture boxes use Tag property to fill it with array index. When handling the click event read the Tag property.
boban.s  Monday, January 22, 2007 1:43 PM
LOL I was thinking of using the Tag property as a workaround, but your method is a lot better than what I was thinking. It seems so obvious now. Thanks for your help boban.s (and Andrej).
gamer6000  Monday, January 22, 2007 2:00 PM
You could also try searching your array and testing equality on each object. I do not know what sort of equality the Image classes uses, though, if it is reference equality or binary equality. Also, this is a less performant approach than using the Tag property ;) It is sometimes appropriate though, usually if you've created your own objects that implement the IEquatable interface, which makes searching through List<T>'s a snap.
Patrick Sears  Monday, January 22, 2007 3:55 PM
Cool. Thanks.
gamer6000  Tuesday, January 23, 2007 5:31 AM

You can use google to search for other answers

Custom Search

More Threads

• Registering File Types
• Throwing exception in multiple threads
• ErrorProvider problem
• Overriding OnPaint on ComboBox
• Generate Excel like Tables with data in Clipboard with C#
• Passing data from one form to the next??
• Calling methods of one form from other
• find window size
• System.ArgumentOutOfRangeException: Length cannot be less than zero.
• MouseLeave event from panel fires for one control within panel but not another control