Delphi - Correctly displaying a Message Dialog in FireMonkey and returning the Modal Result
Question:
It does not show the 2 buttons (Yes, Cancel). Could someone please help me get this right - i.e. correctly show the message dialog with the 2 buttons and send the modal result of the message dialog back as the Result of the function.
The Fmx.TDialogService.MessageDialog
does not support arbitrary combinations of dialog buttons.
Looking into the source code (Fmx.Dialogs.Win.pas) reveals these valid combinations (mbHelp
can be included in all combinations):
- mbOk
- mbOk,mbCancel
- mbYes,mbNo,mbCancel
- mbYes, mbYesToAll, mbNo, mbNoToAll, mbCancel
- mbAbort, mbRetry, mbIgnore
- mbAbort, mbIgnore
- mbYes, mbNo
- mbAbort, mbCancel
This means that [mbYes,mbCancel]
is not a valid combination, use [mbOk,mbCancel]
instead for example.
A final note about the Fmx.TDialogService.MessageDialog
. It is normally a synchronous dialog on desktop applications, but asynchronous on mobile platforms. The use case will look a bit different depending on those conditions, so for a multi-platform application, check the value of TDialogService.PreferredMode.
Hi friend try this code:
function myMessageDialog(const AMessage: string; const ADialogType: TMsgDlgType;
const AButtons: TMsgDlgButtons; const ADefaultButton: TMsgDlgBtn): Integer;
var
mr: TModalResult;
begin
mr:=mrNone;
// standart call with callback anonimous method
TDialogService.MessageDialog(AMessage, ADialogType, AButtons,
ADefaultButton, 0,
procedure (const AResult: TModalResult)
begin
mr:=AResult
end);
while mr = mrNone do // wait for modal result
Application.ProcessMessages;
Result:=mr;
end;
Or this:
function MsgBox(const AMessage: string; const ADialogType: TMsgDlgType; const AButtons: TMsgDlgButtons;
const ADefaultButton: TMsgDlgBtn ): Integer;
var
myAns: Integer;
IsDisplayed: Boolean;
begin
myAns := -1;
IsDisplayed := False;
While myAns = -1 do
Begin
if IsDisplayed = False then
TDialogService.MessageDialog(AMessage, ADialogType, AButtons, ADefaultButton, 0,
procedure (const AResult: TModalResult)
begin
myAns := AResult;
IsDisplayed := True;
end);
IsDisplayed := True;
Application.ProcessMessages;
End;
Result := myAns;
end;
Enjoy it!