Partager via


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 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.

NoteNote:

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
        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.