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为,并将其添加Form到中。The 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单击按钮时,窗体DialogResult的属性将设置为该按钮的。The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.

例如,若要创建 "是/否/取消" 对话框,只需添加三个按钮,并将DialogResult其属性Yes设置No为、 Cancel和。For example, to create a "Yes/No/Cancel" dialog box, simply add three buttons and set their DialogResult properties to Yes, No, and Cancel.

适用于