【发布时间】:2015-03-01 05:17:47
【问题描述】:
所以我正在尝试创建一个简单的程序,当点击它时,它只改变图片框中的图片。我现在只使用两张图片,所以我的图片框点击事件函数的代码 看起来像这样:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
}
由于某种原因,if 语句不起作用,并且图片没有改变。 我该怎么办?
注意:原始代码包含第二个 if 撤消效果的错误,如果条件 将 与 Cyral's answer 建议的修复一起工作,但添加 else 并没有解决问题 - 逐步执行带有else 的代码仍然显示没有匹配任何图像。
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else
pictureBox1.Image = Labirint.Properties.Resources.first;
【问题讨论】:
-
第二个 if 总是被评估;所以它将撤消第一个 if 语句所做的事情。应该将其移至 else 子句。也许还需要调用一个刷新函数。
-
我已经对问题进行了重大更新 - 请检查这是否符合您的意图。请注意,我尝试将问题与 Hans Passant 的答案相匹配,因为它包含实际解释。
标签: c# embedded-resource