我需要在同一个按钮单击事件中实现 MP3 播放器的暂停和恢复事件。以下是我尝试过的代码,它不起作用,任何人都可以给我解决方案
private void button3_Click(object sender, EventArgs e)
{
if (button3.Text == "Pause")
{
CommandString = "pause mp3file";
mciSendString(CommandString, null, 0, 0);
Status = true;
button3.Text = "Resume";
}
if (button3.Text == "Resume")
{
CommandString = "resume mp3file";
mciSendString(CommandString, null, 0, 0);
}
}
最佳答案
您正在更改第一个 if 语句中的 button3.Text 属性。
当测试第二个 if 语句时,它是真的(当 Text 属性为“Pause”时,两个 if 语句都在每次单击按钮时运行)
使用 if,else 来运行一个或另一个代码块。
如果您还希望在第二个代码块上运行测试,请使用 if、else if 语句。
您还应该考虑这些情况都不是真的可能性。
if (button3.Text == "Pause")
{
CommandString = "pause mp3file";
mciSendString(CommandString, null, 0, 0);
Status = true;
button3.Text = "Resume";
}
else if(button3.Text == "Resume")
{
CommandString = "resume mp3file";
mciSendString(CommandString, null, 0, 0);
button3.Text = "Pause";
}
关于c# - c#事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3545673/