窗体的对话框结果是当窗体显示为模式对话框时从该窗体返回的值。如果窗体显示为对话框,用 DialogResult 枚举中的值设置此属性将设置该窗体的对话框结果值、隐藏模式对话框并将控制返回给调用窗体。此属性通常由窗体上 Button 控件的 DialogResult 属性设置。当用户单击 Button 控件时,分配给 Button 的 DialogResult 属性的值将分配给该窗体的 DialogResult 属性。
当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)会隐藏窗体并将 DialogResult 属性设置为 DialogResult.Cancel。当用户单击对话框的“关闭”按钮或设置 DialogResult 属性的值时,不会自动调用 Close 方法。而是隐藏该窗体并可重新显示该窗体,而不用创建该对话框的新实例。因为此行为,所以当应用程序不再需要该窗体时,必须调用该窗体的 Dispose 方法。
可以使用此属性确定对话框是如何关闭的,以便正确处理在该对话框中执行的操作。
public void CreateMyForm()
{
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Make button1's dialog result OK.
button1.DialogResult = DialogResult.OK;
// Make button2's dialog result Cancel.
button2.DialogResult = DialogResult.Cancel;
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
{
// Display a message box indicating that the OK button was clicked.
MessageBox.Show("The OK button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
else
{
// Display a message box indicating that the Cancel button was clicked.
MessageBox.Show("The Cancel button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
}
{
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Make button1's dialog result OK.
button1.DialogResult = DialogResult.OK;
// Make button2's dialog result Cancel.
button2.DialogResult = DialogResult.Cancel;
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
{
// Display a message box indicating that the OK button was clicked.
MessageBox.Show("The OK button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
else
{
// Display a message box indicating that the Cancel button was clicked.
MessageBox.Show("The Cancel button on the form was clicked.");
// Optional: Call the Dispose method when you are finished with the dialog box.
form1.Dispose();
}
}