If picturebox.image == Properties.Resources.ImageA

后端 未结 2 1506
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 09:32

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

2条回答
  •  时光取名叫无心
    2021-01-14 09:47

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

提交回复
热议问题