Updated: October 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)
'Usage
Dim instance As Window
Dim value As Nullable(Of Boolean)
value = instance.DialogResult
instance.DialogResult = value
'Declaration
<TypeConverterAttribute(GetType(DialogResultConverter))> _
Public Property DialogResult As Nullable(Of Boolean)
You cannot set this property in XAML.
When a Window is opened by calling its ShowDialog method, the DialogResult property can be used to determine whether the user accepted (true) or canceled (false) the dialog box. If a dialog box is accepted, this typically indicates to retrieve the data in the dialog box and process it. If a dialog box is canceled, this typically indicates that the data in the dialog box does not have to be processed.
By default, a dialog box is canceled when a user performs one of the following actions:
In all of these cases, DialogResult is false by default.
A dialog box typically provides a special button to cancel a dialog box, which is the button whose IsCancel property is set to true. A button configured this way will automatically close the dialog box when either it is clicked, 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 is clicked or the ENTER key is pressed. However, it will not 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 accept button. Note that DialogResult can only be set when a Window is opened by calling its ShowDialog method.
DialogResult is nullNothingnullptra null reference (Nothing in Visual Basic) when the dialog box is shown but neither accepted nor canceled.
After a dialog box is closed, you can get the dialog box result from the value returned by ShowDialog method, or by inspecting the DialogResult property.
Note: |
|---|
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>
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
Reference
Other Resources
Date | History | Reason |
|---|
October 2009
| Clarified use of DialogResult with ShowDialog. |
Customer feedback.
|
February 2009
| Added information about the null case. |
Customer feedback.
|