简体   繁体   中英

C# sender problems

When i press a picturebox in my project I contact the method card_Pressed. through this code

 this.picturebox.Click += new System.EventHandler(this.card_Pressed)

I have a few pictureboxes as all are linked to this method, now i wanna check which has been pressed by contacting the sender in the method and comparing it to the name of the picturebox.

 if( sender == picturebox1)
{
//something
 }

I got this working when im using a button but not when im using a picturebox, why?

Thanks!

You can use Name property of Picture box, (sender as PictureBox).Name ==... just if you do a null checking it will be better:

var box = (sender as PictureBox);
if (box != null && box.Name == "Blah")
{
  ///
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM