Form.DialogResult 屬性

定義

取得或設定表單的對話方塊結果。

public:
 property System::Windows::Forms::DialogResult DialogResult { System::Windows::Forms::DialogResult get(); void set(System::Windows::Forms::DialogResult value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DialogResult DialogResult { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.DialogResult : System.Windows.Forms.DialogResult with get, set
Public Property DialogResult As DialogResult

屬性值

DialogResult,代表表單在當做對話方塊使用時的結果。

屬性

例外狀況

指定的值超過有效值的範圍。

範例

下列範例會顯示表單做為對話方塊,並顯示訊息方塊,指出表單的 [確定] 或 [取消] 按鈕是藉由參考 DialogResult 表單的 屬性來按一下。

void CreateMyForm()
{
   
   // Create a new instance of the form.
   Form^ form1 = gcnew Form;
   
   // Create two buttons to use as the accept and cancel buttons.
   Button^ button1 = gcnew Button;
   Button^ button2 = gcnew Button;
   
   // Set the text of button1 to "OK".
   button1->Text = "OK";
   
   // Set the position of the button on the form.
   button1->Location = 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 = 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.
      delete form1;
   }
   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.
      delete form1;
   }
}
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();
    }
 }
Public Sub CreateMyForm()
    ' Create a new instance of the form.
    Dim form1 As New Form()
    ' Create two buttons to use as the accept and cancel buttons.
    Dim button1 As New Button()
    Dim button2 As 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 Then
        ' 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
    ' Display a message box indicating that the Cancel button was clicked.
    Else
        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
    End If
End Sub

備註

表單的對話方塊結果是當表單顯示為強制回應對話方塊時,從表單傳回的值。 如果表單顯示為對話方塊,請將此屬性設定為列舉中的值 DialogResult ,會設定表單對話方塊結果的值、隱藏強制回應對話方塊,並將控制項傳回給呼叫表單。 這個屬性通常是由 DialogResult 表單上控制項的 Button 屬性所設定。 當使用者按一下控制項時 Button ,指派給 DialogResult 屬性 Button 的值會指派給 DialogResult 表單的 屬性。

當表單顯示為強制回應對話方塊時,按一下 [ 關閉 ] 按鈕 (表單右上角的 X 按鈕) 會導致表單隱藏,並將 DialogResult 屬性設定為 DialogResult.CancelClose當使用者按一下對話方塊的 [關閉] 按鈕或設定 DialogResult 屬性值時,不會自動呼叫 方法。 相反地,表單會隱藏,而且可以在不建立對話方塊的新實例的情況下再次顯示。 由於此行為,您必須在應用程式不再需要表單時呼叫 Dispose 表單的 方法。

您可以使用這個屬性來判斷對話方塊的關閉方式,以便正確處理對話方塊中執行的動作。

注意

當使用者按一下[關閉] 按鈕時,您可以在表單事件的事件處理常式 Closing 中設定 DialogResult 屬性,以覆寫指派給 DialogResult 屬性的值。

注意

Form如果 顯示為無強制回應視窗,屬性所 DialogResult 傳回的值可能不會傳回指派給表單的值,因為表單的資源會在表單關閉時自動釋放。

適用於

另請參閱