0

I am using Marco Cantu's ShowModal example for Android applications. How do you handle for mrCancel

here is the my code:

procedure TForm1.Button1Click(Sender: TObject);
var
 Form2: TForm2;
begin
 Form2 := TForm2.Create(nil);
 Form2.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
      begin
       ShowMessage('OK');
       Form2.DisposeOf;
      end
     { else
      begin
       ShowMessage('Cancel'); 
       Form2.DisposeOf;
      end;}
      end);
end;

How do I handle the mrCancel? If I have a form with controls the user must enter data in, i would like to give them the option to cancel out of the form and return to the main form.

I should note that I assigned the modalresult to mrCancel for the cancel button, just like i did mrOK for the OK button on the secondary form. The ok button works fine, but if i click the cancel button, the app does nothing and makes it so i cant click the OK button again.

I would have assumed I could have done the following

var
 Form2: TForm2;
begin
 Form2 := TForm2.Create(nil);
 Form2.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
      begin
       ShowMessage('OK');
       Form2.DisposeOf;
      end
      end);
end;

and i should have been able to assign mrCancel to the modalresult property of the cancel button - but it does not work

LuvRAD
  • 118
  • 1
  • 4
  • 13

1 Answers1

3

You have to call DisposeOf() no matter what the ModalResult is. You are currently only calling it if the result is mrOk. Do this instead:

procedure TForm1.Button1Click(Sender: TObject);
var
  Form2: TForm2;
begin
  Form2 := TForm2.Create(nil);
  Form2.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
        ShowMessage('OK')
      else
        ShowMessage('Cancel'); 
      Form2.DisposeOf;
    end
  );
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • your second solution does not work. The cancel showmessage shows before the modal dialog form does. And clicking either button (OK or Cancel) on the dialog terminates the entire app. It would be interesting to know why its calling that portion of code first. The first solution seems to work, but i need to test it more. – LuvRAD Mar 06 '14 at 14:24
  • The showmodals on the main form are causing the issue! I got rid of them and it seems to work fine now – LuvRAD Mar 06 '14 at 15:02
  • 1
    See [Closing modal dialog in delphi firemonkey mobile application (Android)](http://stackoverflow.com/a/26875930/576719) for the correct pattern how to close a modal form in FireMonkey. Instead of DisposeOf, set `Action := TCloseAction.caFree;` in the `OnClose` event of the modal dialog. – LU RD Nov 11 '14 at 22:58