我动态创建几个图片框和EventHandler。当用户点击pictureBox时,程序应删除tрis项。 (用户选择的项目)
我尝试做
for (int i = 1; i <= sunduki; i++)
{
PictureBox PBObj = new PictureBox();
PBObj.Location = new System.Drawing.Point(i * 100, 101);
PBObj.Name = "pictureBox" + i.ToString();
PBObj.Size = new System.Drawing.Size(108, 80);
PBObj.TabIndex = i;
PBObj.BackgroundImage = Image.FromFile(@"syndyk1.jpg");
PBObj.BackgroundImageLayout = ImageLayout.Zoom;
PBObj.Click += new System.EventHandler(pb_Click);
PB.Add(PBObj);
Controls.Add(PB[PB.Count - 1]);}
和pb_click
private void pb_Click(object sender, EventArgs e)
{ PB[this].Visible = false; }
但我有一个错误。 (PB是带有pictureBox的列表)
答案 0 :(得分:2)
sender参数将是已被点击的对象,在这种情况下它是PictureBox对象。
private void pb_Click(object sender, EventArgs e)
{
var pb = sender as PictureBox;
if(pb != null)
{
pb.Visible = false;
}
}
注意:这不会删除图片框,但会使其不可见。控件删除由处理表单处理。