TTaskDialog.Execute always returns True, even clicking Cancel

Run the following code in Delphi XE2 / XE3

with TTaskDialog.Create(Self) do begin try if Execute then ShowMessage('Success') else ShowMessage('Failed'); finally Free; end; end; 

There is no button that you click to close the dialog, the message Success always displayed.

Delphi documentation written by TTaskDialog.Execute as

Use Run to display the Tasks dialog box. Execution opens a task selection dialog that returns true when the user selects a task and clicks Open. If the user clicks Cancel, Execute returns false.

+4
source share
1 answer

The documentation seems to be incorrect, this is the thread of the TTaskDialog.Execute method:

TTaskDialog.Execute → TCustomTaskDialog.Execute → TCustomTaskDialog.DoExecute → TaskDialogIndirect = S_OK?

As you can see, the result of the Execute method is true only if the TaskDialogIndirect function returns S_OK.

To evaluate the result of the dialog, you must use the ModalResult property.

  with TTaskDialog.Create(Self) do begin try if Execute then case ModalResult of mrYes : ShowMessage('Success'); mrCancel : ShowMessage('Cancel'); else ShowMessage('Another button was pressed'); end; finally Free; end; end; 

Note. If you close the dialog with the close button, mrCancel will be returned in the ModalResult property.

+8
source

All Articles