TModalResult when closing a Form with the "TForm.Close"-statement lazarus / 31-LCL ---------------------------------------------------------------------- Relativity · 2015-09-24 07:55 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 ? ---------------------------------------------------------------------- howardpc · 2015-09-24 08:33 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 · 2015-09-24 09:25 Quote from: Relativity on September 24, 2015, 09:55:22 amClicking 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; ---------------------------------------------------------------------- Relativity · 2015-09-25 08:10 Thank you very much, rvk. The problem is solved. ---------------------------------------------------------------------- User137 · 2015-09-27 12:43 You don't have to set ModalResult for button via any piece of code. It can also be set in the Object Inspector window.