我需要在同一个按钮点击事件中实现MP3播放器的Pause和Resume事件.以下是我试过的代码,它不起作用,任何人都可以给我解决方案
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您正在更改第一个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";
}
Run Code Online (Sandbox Code Playgroud)