Delphi - correctly display the message dialog box in FireMonkey and return a modal result

I have a VCL application that I port to FireMonkey. One of the things I came across is that it is MessageDlg(...)deprecated in FireMonkey. I’ll go a little more, I understand that I need to use the method FMX.DialogService.MessageDialog. So I created a function to display the dialog:

function TfMain.GetDeleteConfirmation(AMessage: String): String;
var
  lResult: String;
begin
  lResult:='';
  TDialogService.PreferredMode:=TDialogService.TPreferredMode.Platform;
  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation,
    [ TMsgDlgBtn.mbYes, TMsgDlgBtn.mbCancel ], TMsgDlgBtn.mbCancel, 0,
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        mrYes:    lResult:='Y';
        mrCancel: lResult:='C';
      end;
    end);

  Result:=lResult;
end;

I don’t think I am doing it right, because I’m not sure that I can set a local variable inside an anonymous method, but it still compiles.

I call it this:

  if GetDeleteConfirmation('Are you sure you want to delete this entry?')<>'Y' then
    exit;

When I launched it, a dialog box appears with a message:

enter image description here

2 (, ). -, , - 2- .

Delphi 10.1 Berlin Update 2.

!

EDIT 20170320: @LURD :

function TfMain.GetDeleteConfirmation(AMessage: String): String;
var
  lResultStr: String;
begin
  lResultStr:='';
  TDialogService.PreferredMode:=TDialogService.TPreferredMode.Platform;
  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation,
    FMX.Dialogs.mbYesNo, TMsgDlgBtn.mbNo, 0,
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        mrYes: lResultStr:='Y';
        mrNo:  lResultStr:='N';
      end;
    end);

  Result:=lResultStr;
end;
+4
2

:

2 (, ). -, , - 2- .

Fmx.TDialogService.MessageDialog .

(Fmx.Dialogs.Win.pas) (mbHelp ):

  • Mbok
  • Mbok, mbCancel
  • mbYes, mbNo, mbCancel
  • mbYes, mbYesToAll, mbNo, mbNoToAll, mbCancel
  • mbAbort, mbRetry, mbIgnore
  • mbAbort, mbIgnore
  • mbYes, mbNo
  • mbAbort, mbCancel

, [mbYes,mbCancel] , [mbOk,mbCancel].


Fmx.TDialogService.MessageDialog. , . , TDialogService.PreferredMode.

+1

, :

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;

:

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;

!

0

Source: https://habr.com/ru/post/1672538/


All Articles