2010-08-23 55 views
1

我需要在同一按鈕單擊事件中實現MP3播放器的暫停和恢復事件。以下是我已經嘗試過和它不工作的代碼,可以在任何一個給我的解決方案c#事件的問題

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); 
    } 
} 
+1

什麼不起作用? 「if」條件未驗證或「mciSendString」函數?如果是'mciSendString'函數,你將不得不描述這個函數是什麼,它來自哪裏,它應該做什麼等等。 – 2010-08-23 08:04:49

+2

你有沒有考慮過實現一個狀態機? – DaeMoohn 2010-08-23 08:05:03

+0

*「它不工作」*不幸的是對我們不是很有幫助。你可以說得更詳細點嗎?你有錯誤嗎?暫停工作,但不是簡歷,或其他方式?按鈕失敗,還是MP3?異常,崩潰?當你在事件處理程序中放置一個斷點時會發生什麼,它是否被觸發並且執行是否遵循你期望的路徑? – Abel 2010-08-23 08:06:14

回答

5

你正在改變中的第一if語句的button3.Text財產。 當第二個if語句被測試時,它是真實的(如果語句在Text屬性爲「Pause」時按下每個按鈕都會運行)

使用if,否則運行一個代碼塊或其他代碼塊。

使用if,否則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"; 
} 
+1

+1好的建議(但錯過了回到「暫停」,雖然) – Abel 2010-08-23 08:09:01

+0

編輯包括來自@Arseny答案 – Lewray 2010-08-23 08:10:54

4

乍一看它不會在2的情況下正常工作」的原因

if (button3.Text == "Resume") 
{ 
    CommandString = "resume mp3file"; 
    mciSendString(CommandString, null, 0, 0); 
} 

你錯過了行:

button3.Text = "Pause"; 

其實這不是檢查按鈕是個好主意狀態通過它的文本屬性。作爲一個簡單的解決方案,您需要有一個布爾型標誌來檢查它。

+0

+1發現很好 – Abel 2010-08-23 08:08:07

+0

啊......我錯過了,但注意到兩個如果陳述 – Lewray 2010-08-23 08:09:36

+0

@Lewray它是不是你,而是作者:) – Arseny 2010-08-23 08:12:16

1

你有兩個如果連續的if語句。你只需要一個if/else語句。

改變你的代碼:

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); 
    } 
0

的問題是:

通過你的第二個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