Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing the Modalresult of a button on close
#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


Messages In This Thread
RE: Changing the Modalresult of a button on close - by rlebeau - 09-28-2023, 05:08 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)