开发者

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 pictur开发者_开发知识库ebox, 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")
{
  ///
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜