本文介绍了如果picturebox.image == Properties.Resources.ImageA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了几个按钮(图片框),然后单击它们会更改图像.
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; } }想通了.
需要比较picturebox.image,然后设置picturebox.tag并进行比较.
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"; }推荐答案
您应该保留对资源的本地引用,因为在调用KaminuSkaiciuokle.Properties.Resources...时,您将始终获得对象的新实例:
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; } }更多推荐
如果picturebox.image == Properties.Resources.ImageA
发布评论