0

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

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

4

3 回答 3

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;
    }
 }
于 2012-05-06T09:08:02.850 回答
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;
  }

}

于 2012-05-06T09:15:40.383 回答
0

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

PictureBox lastbox;

然后您可以插入以下代码:

lastbox.Image = Properties.Resources.Image;
lasbox = pb;
于 2012-05-06T09:09:41.573 回答