如果picturebox.image == Properties.Resources.ImageA

编程入门 行业动态 更新时间:2024-10-26 04:19:47
本文介绍了如果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

本文发布于:2023-11-07 01:09:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:image   picturebox   Properties   ImageA   Resources

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!