Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TTaskDialog.Execute always return True even Cancel is clicked

Tags:

delphi

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.

like image 251
Chau Chee Yang Avatar asked Jan 05 '13 04:01

Chau Chee Yang


1 Answers

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.

like image 183
RRUZ Avatar answered Nov 15 '22 07:11

RRUZ