Using FireMonkey Modal Dialog Boxes

From RAD Studio
Jump to: navigation, search

Go Up to FireMonkey Application Design


This topic shows you how to create, configure and display a FireMonkey dialog box. It also shows how to handle its return value using a callback method, and how to free the memory allocated by your modal dialog box after it closes.

Displaying a Modal Dialog Box

Use the following code to display a modal dialog box in your FireMonkey application:

Delphi:

procedure MyCurrentForm.MyButtonClick(Sender: TObject);
var
  dlg: TMyModalForm;
begin
  // Create an instance of a form.
  dlg := TMyModalForm.Create(nil);

  // Configure the form. For example, give it a display name.
  dlg.Caption := 'My Modal Dialog Box';

  // Show your dialog box and provide an anonymous method that handles the closing of your dialog box.
  dlg.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      // Do something.
    end
  );
end;

C++:

1. Define a class that takes the TProc__1 interface, and define a function to handle the closing of your dialog box:
class TModalFormCallback : public TCppInterfacedObject<TProc__1<TModalResult> > {
public:
  TMyModalForm *dlg;
  TMyCurrentForm *MyCurrentForm;

  void __fastcall Invoke(TModalResult ModalResult) {
    // Do something.
  }
};
2. Then pass an instance of this class to ShowModal:
void __fastcall TMyCurrentForm::MyButtonClick(TObject *Sender) {
  // Create an instance of a form.
  TMyModalForm *dlg = new TMyModalForm(NULL);

  // Configure the form. For example, give it a display name.
  dlg->Caption = "My Modal Dialog Box";

  // Create and configure an instance of your callback method.
  TModalFormCallback* ModalFormCallback = new TModalFormCallback();
  ModalFormCallback->dlg = dlg;
  ModalFormCallback->MyCurrentForm = this;

  // Show your dialog box and provide an anonymous method that handles the closing of your dialog box.
  dlg->ShowModal(ModalFormCallback);
}

Freeing Your Modal Dialog Box

You cannot free the memory allocated for your modal dialog box form within the method than handles the closing of your modal dialog box form. To free your modal dialog box form, you must handle its OnClose event as follows:

Delphi:

procedure TMyModalForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := TCloseAction.caFree;
end;

C++:

void __fastcall TMyModalForm::FormClose(TObject *Sender, TCloseAction *Action) {
  Action = TCloseAction::caFree;
}

See Also