Reto Höhener
Reto Höhener

Reputation: 5808

How to close a modal form and all the modal forms that were opened from it?

Real life example: My application has a MainForm. The user can click on a button and login to see the modal AdminSettingsForm. From the AdminSettingsForm, he can open even more modal forms and so on.

When the user logs in to show the AdminSettingsForm, a timer is started, which should close the AdminSettingsForm after a period of inactivity (idleness).

I tried this:

 for i := Screen.FormCount - 1 downto 1 do begin
   if Screen.Forms[i] <> MainForm then begin
     Screen.Forms[i].Close();
   end;
 end;

Nothing happens until I close the topmost modal form, then everything else instantly closes.

Upvotes: 1

Views: 2764

Answers (2)

Numidia Ware
Numidia Ware

Reputation: 21

try this

with Screen do
     for I := FormCount - 1 downto 0 do
       if Forms[I] <> Application.MainForm then
           Forms[I].Close;

Upvotes: 1

Reto H&#246;hener
Reto H&#246;hener

Reputation: 5808

I am sorry, but I think I already found a solution:

 Screen.ActiveForm.Close();

 for i := Screen.FormCount - 1 downto 1 do begin
   if Screen.Forms[i] <> MainForm then begin
     Screen.Forms[i].Close();
   end;
 end;

Seems to work...

Upvotes: 0

Related Questions