Button.DialogResult 屬性

定義

取得或設定當按鈕按下時傳回父表單的值。Gets or sets a value that is returned to the parent form when the button is clicked.

public:
 virtual property System::Windows::Forms::DialogResult DialogResult { System::Windows::Forms::DialogResult get(); void set(System::Windows::Forms::DialogResult value); };
public virtual System.Windows.Forms.DialogResult DialogResult { get; set; }
member this.DialogResult : System.Windows.Forms.DialogResult with get, set
Public Overridable Property DialogResult As DialogResult

屬性值

其中一個 DialogResult 值。One of the DialogResult values. 預設值為 NoneThe default value is None.

實作

例外狀況

所指派的值不是其中一個 DialogResult 值。The value assigned is not one of the DialogResult values.

範例

下列程式碼範例會建立 Button,並將其 DialogResult 屬性設定為 OK,並將其新增至 FormThe following code example creates a Button, sets its DialogResult property to OK, and adds it to a Form.

private:
   void InitializeMyButton()
   {
      // Create and initialize a Button.
      Button^ button1 = gcnew Button;
      
      // Set the button to return a value of OK when clicked.
      button1->DialogResult = ::DialogResult::OK;
      
      // Add the button to the form.
      Controls->Add( button1 );
   }
private void InitializeMyButton()
 {
    // Create and initialize a Button.
    Button button1 = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 
Private Sub InitializeMyButton()
    ' Create and initialize a Button.
    Dim button1 As New Button()
    
    ' Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK
    
    ' Add the button to the form.
    Controls.Add(button1)
End Sub

備註

如果這個屬性的 DialogResult 設定為 None以外的任何專案,而且父表單是透過 ShowDialog 方法來顯示,按一下按鈕就會關閉父表單,而不需要連結任何事件。If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. 然後在按一下按鈕時,表單的 DialogResult 屬性會設定為按鈕的 DialogResultThe form's DialogResult property is then set to the DialogResult of the button when the button is clicked.

例如,若要建立 [是/否/取消] 對話方塊,只需加入三個按鈕,並將其 DialogResult 屬性設為 YesNoCancelFor example, to create a "Yes/No/Cancel" dialog box, simply add three buttons and set their DialogResult properties to Yes, No, and Cancel.

適用於