2013-05-02 84 views
1

我有一個winform應用程序。當所有字段被輸入時,都有一個保存按鈕。 點擊保存按鈕,消息框出現記錄保存成功。該消息框有2個按鈕「是」和「否」。如果是,那麼記錄應該被保存並且表格上的所有字段都應該被清除,如果沒有被點擊,那麼所有的字段都應該在表格上清除而不保存記錄。消息框按鈕上的事件處理程序

+4

Stackoverflow不是免費的代碼寫入服務。請證明你已經嘗試了一些東西。 – 2013-05-02 09:16:47

+2

這很棒,你有什麼麻煩? – 2013-05-02 09:16:51

+6

你忘了問一個問題。 – I4V 2013-05-02 09:16:51

回答

15

MessageBox類的Show方法返回的DialogResult:

DialogResult result = MessageBox.Show("text", "caption", MessageBoxButtons.YesNo); 
if(result == DialogResult.Yes){ 
    //yes... 
} 
else if(result == DialogResult.No){ 
    //no... 
} 
2

DialogResult -enum來處理這樣的事情(從MSDN

private void validateUserEntry5() 
{ 
    // Checks the value of the text. 
    if(serverName.Text.Length == 0) 
    { 
     // Initializes the variables to pass to the MessageBox.Show method. 
     string message = "You did not enter a server name. Cancel this operation?"; 
     string caption = "No Server Name Specified"; 
     MessageBoxButtons buttons = MessageBoxButtons.YesNo; 
     DialogResult result; 
     // Displays the MessageBox. 
     result = MessageBox.Show(this, message, caption, buttons); 
     if(result == DialogResult.Yes) 
     { 
      // Closes the parent form. 
      this.Close(); 
     } 
    } 
} 
1

您可以使用DialogResult Enumeration這一點。

if(MessageBox.Show("Title","Message text",MessageBoxButtons.YesNo) == DialogResult.Yes) 
{ 
//do something 
}