How to: Return a Dialog Box Result

This example shows how to retrieve the dialog result for a window that is opened by calling ShowDialog.

Example

Before a dialog box closes, its DialogResult property should be set with a Nullable<T> Boolean that indicates how the user closed the dialog box. This value is returned by ShowDialog to allow client code to determine how the dialog box was closed and, consequently, how to process the result.

Note

DialogResult can only be set if a window was opened by calling ShowDialog.

            Dim dialogBoxWithResult As New DialogBoxWithResult()
            ' Open dialog box and retrieve dialog result when ShowDialog returns
            Dim dialogResult? As Boolean = dialogBoxWithResult.ShowDialog()
            Select Case dialogResult
                Case True
                    ' User accepted dialog box
                Case False
                    ' User canceled dialog box
                Case Else
                    ' Indeterminate
            End Select
DialogBoxWithResult dialogBoxWithResult = new DialogBoxWithResult();
// Open dialog box and retrieve dialog result when ShowDialog returns
bool? dialogResult = dialogBoxWithResult.ShowDialog();
switch (dialogResult)
{
    case true:
        // User accepted dialog box
        break;
    case false:
        // User canceled dialog box
        break;
    default:
        // Indeterminate
        break;
}

Security

Calling ShowDialog requires permission to use all windows and user input events without restriction.