我制作了几个按钮(图片框),然后您单击它们会更改图像。

我试过这段代码,但它总是跳到其他地方。

图片是从资源加载的。

private void pictureBox7_Click(object sender, EventArgs e)
    {
       if (pictureBox7.Image == KaminuSkaiciuokle.Properties.Resources.IcopalA)
       {
           pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalB;
       }
       else
       {
            pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalA;
       }
    }

想通了。

插入比较picturebox.image 我设置picturebox.tag 并比较。
pictureBox7.Tag = "B";
if (pictureBox7.Tag.ToString() == "A")
   {
        pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalB;
        pictureBox7.Tag = "B";
   }
   else
   {
        pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalA;
        pictureBox7.Tag = "A";
   }

最佳答案

您应该保留对资源的本地引用,因为当您调用 KaminuSkaiciuokle.Properties.Resources... 时,您将始终获得对象的新实例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Bitmap _icopalABitmap = KaminuSkaiciuokle.Properties.Resources.IcopalA;
    Bitmap _icopalBBitmap = KaminuSkaiciuokle.Properties.Resources.IcopalB;

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (pictureBox7.Image == _icopalABitmap)
        {
            pictureBox7.Image = _icopalBBitmap;
        }
        else
        {
            pictureBox7.Image = _icopalABitmap;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox7.Image = _icopalABitmap;
    }
}

关于c# - 如果picturebox.image == Properties.Resources.ImageA,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22364723/

10-13 06:12