If picturebox.image == Properties.Resources.ImageA

一世执手 提交于 2019-12-30 10:36:29

问题


Im making few buttons(picturebox) that then you click them they change image.

I tryed this code but it always skips to else.

Images are loaded from resources.

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

Figured that out.

Insted comparing picturebox.image I set picturebox.tag and compare.

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

回答1:


You should keep local reference to your resources, because when you invoke KaminuSkaiciuokle.Properties.Resources... you will always get new instance of object:

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



回答2:


please attention this code:

Bitmap _icopalABitmap = KaminuSkaiciuokle.Properties.Resources.IcopalA;

_icopalABitmap:it is name you ideal that. KaminuSkaiciuokle: it aqual your project name. IcopalA: is your picture name



来源:https://stackoverflow.com/questions/22364723/if-picturebox-image-properties-resources-imagea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!