的問題是:
通過你的第二個if語句的時候,你已經改變了按鈕的文本,因此,這兩個語句正在運行...
下面是一個簡單的測試:
if (button1.Text == "Pause")
{
label1.Text = label1.Text + " saw pause ";
button1.Text = "Resume";
}
if (button1.Text == "Resume")
{
label1.Text = label1.Text + " saw resume ";
button1.Text = "Pause";
}
回報:LABEL1看到p ause看了簡歷。
有兩種方法來解決這個問題:
你可以插入一個「返回;」每個if語句中的語句:
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";
return;
}
if (button3.Text == "Resume")
{
CommandString = "resume mp3file";
mciSendString(CommandString, null, 0, 0);
button3.Text = "Pause";
return;
}
}
還是其次,你可以捕捉按鈕文本一次的值:
private void button3_Click(object sender, EventArgs e)
{
String value = button3.Text;
if (value == "Pause")
{
CommandString = "pause mp3file";
mciSendString(CommandString, null, 0, 0);
Status = true;
button3.Text = "Resume";
}
if (value == "Resume")
{
CommandString = "resume mp3file";
mciSendString(CommandString, null, 0, 0);
buton3.Text = "Pause"; // As mentioned before, this is required too.
}
}
希望有所幫助。
Steve
什麼不起作用? 「if」條件未驗證或「mciSendString」函數?如果是'mciSendString'函數,你將不得不描述這個函數是什麼,它來自哪裏,它應該做什麼等等。 – 2010-08-23 08:04:49
你有沒有考慮過實現一個狀態機? – DaeMoohn 2010-08-23 08:05:03
*「它不工作」*不幸的是對我們不是很有幫助。你可以說得更詳細點嗎?你有錯誤嗎?暫停工作,但不是簡歷,或其他方式?按鈕失敗,還是MP3?異常,崩潰?當你在事件處理程序中放置一個斷點時會發生什麼,它是否被觸發並且執行是否遵循你期望的路徑? – Abel 2010-08-23 08:06:14