6
votes

Execute 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;

No mater what button you click to close the dialog, the message shown is always Success.

The Delphi documentation written TTaskDialog.Execute as

Use Execute to display the Task Dialog. Execute opens the task-selection dialog, returning true when the user selects a task and clicks Open. If the user clicks Cancel, Execute returns false.

1
It seems like it is an implementation bug or the documentation's error. I have reported the issue to qc.embarcadero.com/wc/qcmain.aspx?d=111722Chau Chee Yang

1 Answers

11
votes

It seems which the documentation is not correct, this is the execution flow of the TTaskDialog.Execute method :

TTaskDialog.Execute -> TCustomTaskDialog.Execute -> TCustomTaskDialog.DoExecute -> TaskDialogIndirect = S_OK?

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

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

  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 using the close button the mrCancel value is returned in the ModalResult property.