我的winform上有Play button
名为btnplay
。我通过图像(play.png)设置背景图像。我想在点击btnplay
时将其背景图像更改为另一个图像(pause.png)。我写这个条件代码,但它不起作用!
string customPath = "../img/";
string playImageFilename = "play.png";
if (btnplay.BackgroundImage == Image.FromFile(Path.Combine(customPath, playImageFilename)))
{...}
答案 0 :(得分:1)
我建议您使用Button Tag
属性。因此,当您加载“播放”背景图片时,请将Tag
属性设置为“播放”,当您将其更改为暂停时,将Tag
设置为“暂停”
因此,在按钮点击事件中,您可以执行以下操作:
string customPath = "../img/";
string playImageFilename = "play.png";
string pauseImageFilename = "pause.png";
if (btnplay.Tag.ToString() == "pause")
{
btnplay.BackgroundImage = Image.FromFile(Path.Combine(customPath, playImageFilename);
btnplay.Tag= "play";
}
else
{
btnplay.BackgroundImage = Image.FromFile(Path.Combine(customPath, pauseImageFilename);
btnplay.Tag= "pause";
}
答案 1 :(得分:0)
在btnPlay
点击事件处理程序之外声明一个变量。
private bool playing = false;
private void btnPlay_Click(object sender, System.EventArgs e)
{
if (playing)
{
// set play image
}
else
{
// set pause image
}
playing = !playing;
}