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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With