11 Vote

Delphi/Lazarus: Abort ModalResult Button and prevent closing Form

Question by Guest | 2015-12-11 at 23:15

I am using ModalResult-Buttons on my Form so that when closing the form, either mrOk or mrAbort will be returned. Accordingly, I am showing the form using the procedure ShowModal.

All of this is working quite well. However, I would like to implement checking whether the data input was correct before clicking on OK. Only in this case, the form should be closed, otherwise an error message should be displayed and the form should remain so that the user can change his input.

Is there any possibility to do a check within the OnClick-Event? I have tried to call abort like it is working in PopUp-events, but this is not working here.

ReplyPositiveNegative
0Best Answer0 Votes

You can just set ModalResult to mrNone within the OnClick-Event. This should do exactly what you want.

For example, you can do it like that:

procedure TForm2.Button1Click(Sender: TObject);
begin
  
  if Edit1.Text = '' then begin
    ShowMessage('Please enter something!');
    ModalResult := mrNone;
  end;

end;

Here, we only want to close the form if the user has written anything into the Edit field. Therefore, we check whether the content of Edit1 is empty and if so, we are showing a message using ShowMessage and we are setting the ModalResult to mrNone preventing the window from closing.
2015-12-12 at 19:47

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.