c# - c#事件的问题

标签 c# .net events

我需要在同一个按钮单击事件中实现 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/

相关文章:

c# web api 2传递字典来上传端点

c# - "Cannot implicitly convert type ' 这个方法<T> ' to ' T '"

c# - 如何防止用户控件填充选项延伸太远

javascript - JavascriptMVC Controller 事件处理程序中的自定义选择?

文件夹上的 C#(outlook 加载项)上下文菜单

c# - 将空字符串绑定(bind)到 Guid.Empty 或避免模型状态错误

c# - 如何在 ios 11 中保存到照片相册崩溃?

c# - WPF 如何使用 StringConverter 将 slider 的值绑定(bind)到标签的内容?

asp.net - 如何在 .net 中读取 Elastic Beanstalk 环境属性?

javascript - 有条件的事件发射器