在 C# 表单中,我创建了一个函数,该函数一旦被调用,就会创建一个具有所需图像、大小和位置的图片框:
private void NewPic(string nName, int locX, int locY, int SizX, int SizY, Image Img)
{
PictureBox Pic = new PictureBox();
Pic.Name = nName; Pic.Image = Img;
Pic.BackColor = Color.Transparent;
Pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
Pic.Size = new System.Drawing.Size(SizX, SizY);
Pic.Location = new System.Drawing.Point(locX, locY);
Controls.Add(Pic);
Pic.Click += new EventHandler(Pic_Click);
}
现在,当我需要一张图片时,我会这样做:
NewPic("FIRE", 32, 100, 120, 120, Properties.Resources.Image);
问题是,在单击事件中,当我单击图片框时,我希望它更改其背景图像,但是,如果我单击其他一些图片框,我希望最后一个图片框自行重置:
private void Pic_Click(object sender, System.EventArgs e)
{
PictureBox pb = (PictureBox)sender;
switch (pb.Name)
{
case "1":
pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
pb.BackgroundImageLayout = ImageLayout.Stretch;
//INSERT CODE HERE: to remove from other if it has
break;
case "2":
pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
pb.BackgroundImageLayout = ImageLayout.Stretch;
//INSERT CODE HERE: to remove from other if it has
break;
}
}
我需要可以应用于多个图片框/对象的代码,而不仅仅是两个