我有一个WinForms应用程序,当输入所有字段时,有一个保存按钮。
单击保存按钮时,出现一个消息框,说明"记录保存成功"。消息框有两个按钮,"是"one_answers"否"。
如果是,则应保存该记录,并清除表单上的所有字段。如果单击no,则应该清除表单上的所有字段,而不保存记录。
我如何实现这个?
你不需要一个事件处理程序;MessageBox类的Show方法返回一个对话结果:
DialogResult result = MessageBox.Show("text", "caption", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes){
//yes...
}
else if(result == DialogResult.No){
//no...
}
有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();
}
}
}
您可以使用dialgresult Enumeration。
if(MessageBox.Show("Title","Message text",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//do something
}