2013-05-02 90 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 
}