Recent

Author Topic: Modal Form OnClose ModalResult not yet assigned?  (Read 10021 times)

99Percent

  • Full Member
  • ***
  • Posts: 160
Modal Form OnClose ModalResult not yet assigned?
« on: March 20, 2013, 08:41:50 pm »
I am trying to execute code on a modal form but only when the ModalResult=mrOk

At OnClose event of the form doesn't seem to have the ModalResult assigned yet so I cannot distinguish here if the form is closing with such modal result. I do not want to execute this code if the user is cancelling the form.

I have code on the TBitBtn with Kind=bkOk on click event, but this is not executed if the user presses enter elsewhere on the form.

Executing the code outside the form seems cumbersome at best.

Please help

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Modal Form OnClose ModalResult not yet assigned?
« Reply #1 on: March 20, 2013, 11:15:50 pm »
Set ModalResult explicitly in the form to mrCancel at creation.
Then use the form's OnCloseQuery event to check the value of ModalResult at closing.
Insert a simple test such as
Code: [Select]
if (ModalResult = mrOk)
 then canClose := True
else canClose := False;

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Modal Form OnClose ModalResult not yet assigned?
« Reply #2 on: March 20, 2013, 11:39:38 pm »
setting modalresult to any value should automatically close the modal form and stop the modal loop to use it to hold some kind of value is not a safe tactic and should be avoided.

Pressing enter anywhere on the form should not close the form automatically there must be user code that takes care of that and not knowing how your form is handling it makes it impossible for us to propose any solution.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

99Percent

  • Full Member
  • ***
  • Posts: 160
Re: Modal Form OnClose ModalResult not yet assigned?
« Reply #3 on: March 21, 2013, 01:16:12 am »
setting modalresult to any value should automatically close the modal form and stop the modal loop to use it to hold some kind of value is not a safe tactic and should be avoided.

Pressing enter anywhere on the form should not close the form automatically there must be user code that takes care of that and not knowing how your form is handling it makes it impossible for us to propose any solution.
The form has a TBitBtn with ModalResult property set to mrOk, This causes the form to intercept keystrokes (even with KeyPreview property set to false) and close it if Enter key is hit. If I remove the button or set the ModalResult property to mrNone then that behavior stops

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Modal Form OnClose ModalResult not yet assigned?
« Reply #4 on: March 21, 2013, 01:23:09 am »
that is only possible if the tbitbtn.Default property is true and if that is so then when the buttons result is set to mrnone hitting enter should execute the button's onclick event where you can execute the code needed and at the end set the modalresult to mrok manually to close the form.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

99Percent

  • Full Member
  • ***
  • Posts: 160
Re: Modal Form OnClose ModalResult not yet assigned?
« Reply #5 on: March 21, 2013, 05:29:11 am »
that is only possible if the tbitbtn.Default property is true and if that is so then when the buttons result is set to mrnone hitting enter should execute the button's onclick event where you can execute the code needed and at the end set the modalresult to mrok manually to close the form.
I had set the TBitBtn Default Property to true and that was screwing up my program flow. I set it to false and all is fixed now.

Thanks!

 

TinyPortal © 2005-2018