Forum > Cocoa

ShowModal: Setting ModalResult, or calling "close", doesn't close form

(1/2) > >>


I have a project, where one Form uses ShowModal to open another form.
The second form closes when a certain task has been completed.
* Attached a test project where I'm using a timer to close the form after 5 seconds.

This works very well, and as expected, under Windows and Linux, however not under Cocoa.
Under Cocoa the user will have to move the mouse for the form to close (just move, not even click or anything like it). I've tried setting ModalResult (even setting it to mrCancel in the OnCreate event), tried calling "Close", tried all kinds of combinations - none of it works.

I've seen discussions about this, with some saying that the user is expected to interact in some way with he form, since "this is how modalresult works".
This may very well be true, but that means that there still is a bug ... either Cocoa isn't behaving as expected, or Linux and Windows are not behaving as expected.

Or maybe I'm doing something wrong here?

I downloaded and checked the test project. Indeed, it compiles and runs as described by the author. I use:

- OS: Windows 10 64-bit (2018),
- IDE: Lazarus 2.2.0 (rev lazarus_2_2_0) FPC 3.2.2 x86_64-win64-win32 / win64).

Thank you VisualLab for the confirmation - much appreciated! 👍🏻

Why do you use Close; and then ModalResult := mrOK inside Timer1StopTimer ?
Use only ModalResult := mrOK and see if that works.

Thank you Zeljko for chiming in!  :)

Apologies, this was a left-over from testing all kinds of combinations.

Unfortunately, just using ModalResult:=mrOK doesn't work either - that's what I started with (see code below).
Calling Close (instead of setting ModalResult) didn't work either, so that's when I started trying all kinds of weird things to see what will work.

p.s. The FormClose event does not seem to get fired fired either, until user moves the mouse.

So the code below results in exact the same issue.
It's almost like the mouse needs to be moved to get the message that the window needs to be closed to be processed.
(I have hardly any experience with the entire messaging in the background - so my apologies if I'm describing this wrong)

- Everything I tried works under Linux and Windows. Just not under Cocoa.
- I forgot to mention that I've tested this with several Lazarus/FPC versions, Intel and AARCH, and Monterey and Ventura.
  Last version I tried is: Lazarus 2.3.0 (rev main-2_3-2069-g4aa7b5b350) FPC 3.2.2 x86_64-darwin-cocoa

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode ObjFPC}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type   { TForm2 }   TForm2 = class(TForm)    Timer1: TTimer;    procedure FormShow(Sender: TObject);    procedure Timer1StartTimer(Sender: TObject);    procedure Timer1StopTimer(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private    Counter:integer;  public   end; var  Form2: TForm2; implementation {$R *.lfm} { TForm2 } procedure TForm2.FormShow(Sender: TObject);begin  Timer1.Enabled:=true;end; procedure TForm2.Timer1StartTimer(Sender: TObject);begin  Counter := 0;  self.caption := 'Timer start';end; procedure TForm2.Timer1Timer(Sender: TObject);begin  inc(Counter);  self.caption := 'Timer '+IntToStr(Counter);  if Counter>5 then Timer1.Enabled:=false;end; procedure TForm2.Timer1StopTimer(Sender: TObject);begin  self.caption := 'Timer done';  ModalResult:=mrOK;end; end.  


[0] Message Index

[#] Next page

Go to full version