This documentation is archived and is not being maintained.

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.

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.


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

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
    case false:
        // User canceled dialog box
        // Indeterminate

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