Search code examples
delphivcl

Conditionally prevent OK button from closing dialog


I have a dialog with some fields in it. The user is suppose to fix the form which will later on be parsed. When the user presses ok, the data goes to the database if the checks are successful, if not a warning should be shown and the data dialog should stay. Something like shown below:

procedure TDataSaver.OKBtnClick(Sender: TObject);
begin
    if checkData then
        saveDataInDatabase
    else
        …prevent from closing code…
end;

Solution

  • Use OKBtn.ModalResult := mrNone as default value and

    procedure TDataSaver.OKBtnClick(Sender: TObject);
    begin
      if checkData then
        ModalResult := mrOK;
    end;