CancelCommandIndex | cancelCommandIndex property

MessageDialog.CancelCommandIndex | cancelCommandIndex property

Gets or sets the index of the command you want to use as the cancel command. This is the command that fires when users press the ESC key.

Add the commands before you set the index.


Public Property CancelCommandIndex As UInteger

Property value

Type: Number [JavaScript] | System.UInt32 [.NET] | uint32 [C++]

The index of the cancel command.


Use message dialogs to send critical or blocking messages and questions from the app.

ValueWhen to use

The dialog offers a safe default choice that is the equivalent of cancellation, like "Cancel" or "Close." Set CancelCommandIndex to the index of the command handler for that cancel/close command, so that when the user dismisses the dialog through a noncommital action, like pressing ESC, the API returns the command handler you want.

Note  Generally, you should avoid creating dialogs that can be dismissed this way and that re-launch asking the same question or sending the same message again and again. They make the app noisy and tend to annoy users.

The user is required to make an explicit decision like tapping a specific button on the dialog. This ensures that the user can't dismiss the dialog through a noncommital action like pressing ESC.

-2Not recommended.

The dialog is not dismissed when the user presses ESC or during an incoming contract activation; however, if the app re-uses the main app window when responding to incoming activations, the dialog's command handlers will no longer be valid. Because the API doesn't handle this behavior, we recommend not using this value.


Error handling on dismissal by a contract activation

If the app receives an incoming contract activation (like from Search, Share, Settings, Devices or the file picker contracts) while the dialog is showing, the dialog is programmatically dismissed, regardless of the property value that has been set for the CancelCommandIndex.

To help you handle this case, the API returns a dummy command handler. You, then, can decide how to handle and react to the error.

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Popups [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Popups [C++]



See also




© 2016 Microsoft