Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Window Class
Window Properties
 DialogResult Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Window..::.DialogResult Property

Updated: February 2009

Gets or sets the dialog result value, which is the value that is returned from the ShowDialog method.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
Visual Basic (Declaration)
<TypeConverterAttribute(GetType(DialogResultConverter))> _
Public Property DialogResult As Nullable(Of Boolean)
Visual Basic (Usage)
Dim instance As Window
Dim value As Nullable(Of Boolean)

value = instance.DialogResult

instance.DialogResult = value
C#
[TypeConverterAttribute(typeof(DialogResultConverter))]
public Nullable<bool> DialogResult { get; set; }
Visual C++
[TypeConverterAttribute(typeof(DialogResultConverter))]
public:
property Nullable<bool> DialogResult {
    Nullable<bool> get ();
    void set (Nullable<bool> value);
}
JScript
public function get DialogResult () : Nullable<boolean>
public function set DialogResult (value : Nullable<boolean>)
XAML
You cannot set this property in XAML.

Property Value

Type: System..::.Nullable<(Of <(Boolean>)>)
A Nullable<(Of <(T>)>) value of type Boolean. The default is false.
ExceptionCondition
InvalidOperationException

DialogResult is set before a window is opened by calling ShowDialog, or a window opened by calling Show.

DialogResult can be used from the code that showed a dialog box to determine whether a user accepted (true) or canceled (false) the dialog box. If a dialog box was accepted, this signifies to the code that opened the dialog box to retrieve the data that was collected by the user and process it. If a dialog box was canceled, however, this signifies that calling code should stop any further processing.

By default, a dialog box is canceled when a user does one of the following:

  • PressesALT+F4.

  • Clicks the Close button.

  • Selects Close from the System menu.

In all of these cases, DialogResult is false by default.

A dialog box typically provides a special button to cancel a dialog, which is the button whose IsCancel property is set to true. A button configured this way will automatically close a window when either it is pressed, or when the ESC key is pressed. In either of these cases, DialogResult remains false.

A dialog box also typically provides an accept button, which is the button whose IsDefault property is set to true. A button configured this way will raise its Click event when either it or the ENTER key is pressed. However, it won't automatically close the dialog box, nor will it set DialogResult to true. You need to manually write this code, usually from the Click event handler for the default button.

DialogResult is nullNothingnullptra null reference (Nothing in Visual Basic) when the dialog box is shown but neither accepted nor canceled.

After a dialog box closes, you can get the dialog result from the value returned by ShowDialog method, or by inspecting the DialogResult property.

DialogResult can only be set when a Window is opened by calling its ShowDialog method.

NoteNote:

You cannot set or get this property when a window is hosted in a browser.

The following example shows how to configure an OK button and a Cancel button to return the appropriate DialogResult.

<Button IsDefault="True" Click="acceptButton_Click">OK (IsDefault=True)</Button>
<Button IsCancel="True">Cancel (IsCancel=True)</Button>

C#
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    public partial class DialogBox : Window
    {
        public DialogBox()
        {
            InitializeComponent();
        }

        // The accept button is a button whose IsDefault property is set to true.
        // This event is raised whenever this button is clicked, or the ENTER key
        // is pressed.
        void acceptButton_Click(object sender, RoutedEventArgs e)
        {
            // Accept the dialog and return the dialog result
            this.DialogResult = true;
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

February 2009

Added information about the null case.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker