Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DialogResult Property
Collapse the table of content
Expand the table of content

Button.DialogResult Property

Gets or sets a value that is returned to the parent form when the button is clicked.

[Visual Basic]
Public Overridable Property DialogResult As DialogResult  _
   Implements IButtonControl.DialogResult
[C#]
public virtual DialogResult DialogResult {get; set;}
[C++]
public: __property virtual DialogResult get_DialogResult();
public: __property virtual void set_DialogResult(DialogResult);
[JScript]
public function get DialogResult() : DialogResult;
public function set DialogResult(DialogResult);

Property Value

One of the DialogResult values. The default value is None.

Implements

IButtonControl.DialogResult

Exceptions

Exception Type Condition
InvalidEnumArgumentException The value assigned is not one of the DialogResult values.

Remarks

If the value of this property is set to anything other than DialogResult.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. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.

For example, to create a "Yes/No/Cancel" dialog, simply add three buttons and set their DialogResult properties to DialogResult.Yes, DialogResult.No, and DialogResult.Cancel.

Example

The following example creates a Button, sets its DialogResult property to DialogResult.OK, and adds it to a Form.

[Visual Basic] 
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 'InitializeMyButton

[C#] 
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);
 }
 

[C++] 
private:
    void InitializeMyButton() {
        // Create and initialize a Button.
        Button __gc *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);
    }; 

[JScript] 
private function InitializeMyButton()
 {
    // Create and initialize a Button.
    var button1 : Button = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = System.Windows.Forms.DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Button Class | Button Members | System.Windows.Forms Namespace

Show:
© 2015 Microsoft