简体   繁体   中英

Delphi XE5 Anonymous ShowModal doesn't work as expected

I am a newbie to Delphi XE5 and currently developing Android platform applications on my Windows desktop using Delphi XE5. I have two forms(Form1 and Form2) and tried to show Form2 in modal way on Form1 according to the way showed in Marco's RAD Blog( http://blog.marcocantu.com/blog/xe5_anonymous_showmodal_android.html ).

But result was not as expected.

procedure TForm1.Button1Click(Sender: TObject);
var
  frm2: TForm2;
begin
  frm2 := TForm2.Create(nil);

  ShowMessage('before frm2.ShowModal...');
  frm2.ShowModal (
    procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
        if frm2.ListBox1.ItemIndex >= 0 then
          edit1.Text := frm2.ListBox1.Items [frm2.ListBox1.ItemIndex];
      frm2.DisposeOf;
    end
  );
  ShowMessage('after frm2.ShowModal...');
end;

I wrote above code and run the application on an Android device. I clicked the Button1, then I got the messagebox "before frm2.ShowModal... ", next "after frm2.ShowModal...", and then Form2 was showed.

I expect that the order should be 1)"before frm2.ShowModal... " message, 2) Form2 being showed, and 3) "after frm2.ShowModal..." message.

What's wrong with me?

The call to the anonymous ShowModal is not blocking, which means that any code after the ShowModal will be executed first.

One note here. Calling frm2.DisposeOf is wrong. You must use this pattern:

declare

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

See http://www.malcolmgroves.com/blog/?p=1585 . The documentation has been corrected in XE7, Using FireMonkey Modal Dialog Boxes , but this pattern can be used in all Delphi versions.


Conclusion : if you want to execute code after the modal dialog is closed, put that code inside the anonymous method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM