http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.form.dialogresult(VS.80).aspx
表單的對話方塊結果是當表單顯示為強制回應對話方塊時從該表單返回的值。如果表單顯示為對話方塊,用 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();
}
}