Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing the Modalresult of a button on close
#1
I feel stupid to have to ask this after all these years programming, but I never got this properly sorted out.

I have a Form which is opened with ShowModal.
There is a Cancel button and an OK button.
The Modalresult Cancel button is set at design time to mrCancel.

But the Modalresult of the OK botton cannot be set to mrOK because I need to be able to break out of it, and let the user correct all invalid settings on the form.
So:

OkBtn.Modelresult = mrNone;

Form.OkBtnClick();
begin
  if SomethingWrong then
  begin
     ShowMessage(Fix it!);
     OkBtn.Modelresult := mrNone;
     exit; // Form should NOT close
  end else
  begin
     All okay, Close Form with Modalresult mrOK;
     OkBtn.Modelresult := mrOK; // Nothing happens...
     Form.Modalresult := mrOK; // has no effect
     CLose; // Does NOT close with mrOK.
  end;

This does not work.
What is the correct way to deal with a Modal form in such a way that the final Modalresult is mrOK but while still working it is possible to intercept the OKBtn from closing the form?
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#2
(09-26-2023, 08:25 PM)BartKindt Wrote: But the Modalresult of the OK botton cannot be set to mrOK because I need to be able to break out of it, and let the user correct all invalid settings on the form.

Actually, you can.

The Button's ModalResult value is assigned to the Form's ModalResult property before the Button's OnClick event is fired.  The Form's ModalResult is not processed until after the OnClick handler exits.  So, setting the Button's ModalResult inside the OnClick handler has no effect on the Form whatsoever.

Thus, you can set the Button's ModalResult to mrOK at design-time, and then set the Form's ModalResult to mrNone if you need to block the closure from the OnClick handler.

But, from a design perspective, it makes more sense to have the Button's ModalResult set to mrNone at design-time, and then set the Form's ModalResult to mrOK in the OnClick event.

(09-26-2023, 08:25 PM)BartKindt Wrote:
Code:
OkBtn.Modelresult := mrOK; // Nothing happens...
Form.Modalresult := mrOK; // has no effect
CLose; // Does NOT close with mrOK.

This does not work.

Setting Form.ModalResult to anything other than mrNone (including calling Form.Close, which sets the Form's ModalResult to mrCancel) WILL most definitely close the Form and cause ShowModal() to exit.

The only way I can think of for that not working is if you are setting the ModalResult of the wrong Form object, or if the Form is not being shown modally.

(09-26-2023, 08:25 PM)BartKindt Wrote: What is the correct way to deal with a Modal form in such a way that the final Modalresult is mrOK but while still working it is possible to intercept the OKBtn from closing the form?

See above.

Reply
#3
Indeed.
If I ignore the Button's Modalresult completely and manually set the Form modalresult, then it actually works.
It is weird, because in the past I have tried this before and then the Buttons's Modalresult would override the Form Modalresult (which it is supposed to..?) and because I cannot set the Button Modalresult during the OnClick, it never worked.
Now I wrote a test program, and yes, it does work if I set the *Form* Modalresult in the OKBtn OnClick event, and the OKBtn Modelresult does NOT override it.
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#4
(09-28-2023, 06:09 PM)BartKindt Wrote: It is weird, because in the past I have tried this before and then the Buttons's Modalresult would override the Form Modalresult (which it is supposed to..?) and because I cannot set the Button Modalresult during the OnClick, it never worked.

It has always been the case that a Button's ModalResult is just a default. When the Button is clicked, the closure process begins with the Button's ModalResult assigned as the Form's initial ModalResult, but user code has always gotten the final say in whether the Form actually closes and what its final ModalResult would be.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)