动态改变图片框

本文关键字:改变 动态 | 更新日期: 2023-09-27 18:05:52

我动态创建几个图片框,以及eventandler。当用户点击pictureBox时,程序应该删除图片项。(用户选择的项)

i try do

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]);}

and in pb_click

private void pb_Click(object sender, EventArgs e)
    { PB[this].Visible = false; }

但是我有一个错误。(PB为带有pictureBox的列表)

动态改变图片框

sender参数将是被点击的对象,在本例中是PictureBox对象。

private void pb_Click(object sender, EventArgs e)
{ 
   var pb = sender as PictureBox;
   if(pb != null)
   {
       pb.Visible = false;
   }
}

注意:这不会删除图片框,但使其不可见。控件的删除由窗体的处置来处理。