【问题标题】:button.BackgroundImage = <pngfile> does nothingbutton.Background Image = <png file> 什么都不做
【发布时间】:2012-01-19 23:14:49
【问题描述】:

我想在单击一次时更改按钮的 BackgroundImage,然后在再次单击时将其更改回原始图像(它会一遍又一遍地工作)。我的代码 sn-p 是这样的:

private void handButton_Click(object sender, EventArgs e)
    {
        if (handButton.BackgroundImage == WindowsFormsApplication1.Properties.Resources.Hands_Right)
        {
            handButton.BackgroundImage = WindowsFormsApplication1.Properties.Resources.Hands_Left;
        }
        else if (handButton.BackgroundImage == WindowsFormsApplication1.Properties.Resources.Hands_Left)
        {
            handButton.BackgroundImage = WindowsFormsApplication1.Properties.Resources.Hands_Right;
        }
    }

但是当我运行程序并单击按钮时;没发生什么事。图像是 32x32,我可以清楚地看到原始图像。单击时,原始图像将保留在那里。没有其他变量影响这个 sn-p(至少,搜索“handButton”只能从这个 sn-p 得到结果)。

有什么建议吗?我没有错误,所以我怀疑我做错了。有没有更好的方法来回更改图像?

【问题讨论】:

  • 只是检查一下,点击事件处理了吗?单步执行代码时会发生什么?原始图像是什么?如果它不是左或右,它永远不会设置。
  • 对不起,我不明白你的意思。我可以拉出一些窗口来显示执行的代码吗?

标签: c# image button png


【解决方案1】:

Properties.Resources 类并不像您想象的那样工作。像 Hands_Right 这样的属性实际上返回一个 new 位图,而不是之前返回的任何对象。这不会很好地工作,因为修改位图也会修改其设计中的属性。

所以你的 if() 语句表达式永远不会计算为真。单独跟踪按钮状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    相关资源
    最近更新 更多