C#更改多个控件的背景图像

时间:2012-05-06 08:59:45

标签: c# winforms

在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;
        }
     }

我需要可以应用于多个图片框/对象的代码,而不仅仅是两个

3 个答案:

答案 0 :(得分:1)

最简单的方法是在表单中添加一个成员,该成员将跟踪之前单击的PictureBox:

PictureBox _lastPictureBox = null;

在处理程序中,检查_lastPictureBox是否有值,并根据需要进行更新:

private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    if (_lastPictureBox != null) 
    {
        // update the previous one, eg:
        _lastPictureBox.BackgroundImage = Properties.Resources.FirstImg;
    }

    // now set it to the current one:
    _lastPictureBox = pb;

    switch (pb.Name)
    {
    case "1": 
        pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
        pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
    case "2":
        pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
        pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
    }
 }

答案 1 :(得分:1)

我认为您还需要存储最后一个图片框的图像

PictureBox _lastPictureBox = null;
Image _lastPictureBoxImage = null;

private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    if (_lastPictureBox != null) 
    {
      // update the previous one, eg:
       _lastPictureBox.BackgroundImage = _lastPictureBoxImage;
    }

    // now set it to the current one:
   _lastPictureBox = pb;
   _lastPictureBoxImage = pb.Image;
   switch (pb.Name)
   {
     case "1": 
       pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
       pb.BackgroundImageLayout = ImageLayout.Stretch;
    break;
    case "2":
      pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
      pb.BackgroundImageLayout = ImageLayout.Stretch;
    break;
  }

}

答案 2 :(得分:0)

如果我理解正确,你需要一个全局变量

PictureBox lastbox;

然后你可以插入这段代码:

lastbox.Image = Properties.Resources.Image;
lasbox = pb;