Recent

Author Topic: TModalResult when closing a Form with the "TForm.Close"-statement  (Read 7067 times)

Relativity

  • Full Member
  • ***
  • Posts: 103
TModalResult when closing a Form with the "TForm.Close"-statement
« on: September 24, 2015, 09:55:22 am »
I have a primary TForm (Form1) that opens a secondary one (Form2) through "Form2.ShowModal;".
Clicking a button in the second TForm, the TForm is closed through "Close;".
Thereafter I would expect in the primary form to get Form2.ModalResult = mrClose, that is = 11.
Instead, I find Form2.ModalResult = 2 (= mrCancel).
And actually, also when I close the second TForm using the X in the top right corner, I invariably get Form2.ModalResult = 2, whereas I would expect Form2.ModalResult = mrAbort.

Can anybody explain this ?

"How'm I gonna get through?"
  -- Pet Shop Boys

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TModalResult when closing a Form with the "TForm.Close"-statement
« Reply #1 on: September 24, 2015, 10:33:20 am »
The source code, as always, shows you what happens behind the scenes when a form is hidden.
This is from customform.inc, line 2917:

Code: [Select]
   function TCustomForm.Showmodal: integer;
    ...
     finally
        { guarantee execution of widgetset CloseModal }
        TWSCustomFormClass(WidgetSetClass).CloseModal(Self);
        // set our modalresult to mrCancel before hiding.
        if ModalResult = 0 then
          ModalResult := mrCancel;
        // We should always re-enabled the forms before issuing Hide()
        // Because otherwise we will for a short amount of time have
        // all forms disabled, and some systems, like WinCE, will interprete this
        // as a problem in the application and hide it.
        // See bug 22718
        Screen.EnableForms(DisabledList);
        Hide;
        RestoreFocusedForm;
      end;
   ...                                     

rvk

  • Hero Member
  • *****
  • Posts: 6922
Re: TModalResult when closing a Form with the "TForm.Close"-statement
« Reply #2 on: September 24, 2015, 11:25:49 am »
Clicking a button in the second TForm, the TForm is closed through "Close;".
And why are you using "Close;" in a button for a ModalForm??

Setting ModalResult := something will automatically close that form with the correct Result. (No need to call Close manually)

Edit: B.T.W. if the form is used for both Modal and non-modal you can do something like this:

Code: [Select]
procedure TForm1.CloseClick(Sender: TObject);
begin
  if fsModal in FFormState then
    ModalResult := mrClose
  else
    Close;
end;
« Last Edit: September 24, 2015, 11:43:50 am by rvk »

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: TModalResult when closing a Form with the "TForm.Close"-statement
« Reply #3 on: September 25, 2015, 10:10:25 am »
Thank you very much, rvk.
The problem is solved.
"How'm I gonna get through?"
  -- Pet Shop Boys

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TModalResult when closing a Form with the "TForm.Close"-statement
« Reply #4 on: September 27, 2015, 02:43:23 pm »
You don't have to set ModalResult for button via any piece of code. It can also be set in the Object Inspector window.

 

TinyPortal © 2005-2018