Question

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

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top