Partilhar via


ChildWindow.DialogResult Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a value that indicates whether the ChildWindow was accepted or canceled.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls (in System.Windows.Controls.dll)

Syntax

'Declaration
<TypeConverterAttribute(GetType(NullableBoolConverter))> _
Public Property DialogResult As Nullable(Of Boolean)
[TypeConverterAttribute(typeof(NullableBoolConverter))]
public Nullable<bool> DialogResult { get; set; }

Property Value

Type: System.Nullable<Boolean>
true if the child window was accepted; false if the child window was canceled. The default is nulla null reference (Nothing in Visual Basic).

Remarks

A ChildWindow is typically used as a dialog box and provides a DialogResult value. DialogResult can be used from the code that showed the ChildWindow 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 child window to retrieve the data that was collected from the user and process it. If a dialog box was canceled, this signifies that the calling code should not process data from the child window.

Setting the DialogResult value automatically closes the child window. Closing the child window in any other way, including calling the Close method, pressing CTRL+SHIFT+F4, or clicking the close button, will set DialogResult to false. DialogResult is nulla null reference (Nothing in Visual Basic) as long as the child window is open.

To get the DialogResult value from a child window, handle the Closed event in the code-behind page of the calling window. In the Closed event handler, cast the sender parameter to a ChildWindow or a derived class to access the DialogResult property.

Examples

The following code example demonstrates how to set the DialogResult value in the click events of the OK and Cancel buttons of a ChildWindow. This example is part of a larger example available in the ChildWindow class overview.

Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles OKButton.Click
    Me.DialogResult = True
End Sub

Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles CancelButton.Click
    Me.DialogResult = False
End Sub
private void OKButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;
}

The following example demonstrates how to get the DialogResult value in the Closed event handler. This code is in the page that called the ChildWindow. This example is part of a larger example available in the ChildWindow class overview.

Private Sub loginWnd_Closed(ByVal sender As System.Object, ByVal e As EventArgs)
    Dim lw As LoginWindow = CType(sender, LoginWindow)

    If lw.DialogResult = True AndAlso lw.nameBox.Text <> String.Empty Then
        Me.helloTxt.Text = "Hello " + lw.nameBox.Text
    ElseIf (lw.DialogResult = False) Then
        Me.helloTxt.Text = "Login canceled."
    End If
End Sub
void loginWnd_Closed(object sender, EventArgs e)
{
    LoginWindow lw = (LoginWindow)sender;

    if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
    {
        this.helloTxt.Text = "Hello " + lw.nameBox.Text;
    }
    else if (lw.DialogResult == false)
    {
        this.helloTxt.Text = "Login canceled.";
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.