Recent

Author Topic: [SOLVED] Stop the OKButton  (Read 2199 times)

nana232

  • New Member
  • *
  • Posts: 40
[SOLVED] Stop the OKButton
« on: April 29, 2017, 10:51:59 am »
I use TButtonPanel in my sub-form.
I have some condition to identify my result within the "OKButtonClick" Event.

When OK Button is clicked
if Condition = True >>> close form.
if Condition = False >>> I want the form is still show.

But the problem is that the form is closed for all condition.

How to break the OKButton?
« Last Edit: April 29, 2017, 01:07:50 pm by nana232 »
Lazarus 1.8.4 Win10 32bit

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: Stop the OKButton
« Reply #1 on: April 29, 2017, 11:10:37 am »
The OKButton sets the Modalresult of the form to mrOK and this closes the form; the form remains open while its Modalresult is mrNone.

So, in your validation in the OKButton's OnClick you must just simply reset ModalResult back to mrNone to keep the form open:

Code: Pascal  [Select][+][-]
  1. procedure Form1.OKButtonClick(Sender: TObject);
  2. begin
  3.   if SomeErrorCondition  then begin
  4.     ModalResult := mrNone;
  5.     ShowMessage('An error has occured. The form cannot be closed.');
  6.   end;
  7.   // No need to do anything if no error had occured - the button will close the form automatically.
  8. end;
« Last Edit: April 29, 2017, 12:33:14 pm by wp »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Stop the OKButton
« Reply #2 on: April 29, 2017, 11:21:05 am »
A small example using a TButtonPanel is attached.

nana232

  • New Member
  • *
  • Posts: 40
Re: Stop the OKButton
« Reply #3 on: April 29, 2017, 01:06:45 pm »
Thank you  :)
Lazarus 1.8.4 Win10 32bit

 

TinyPortal © 2005-2018