Closing unknown modal form(s) - Delphi

Chris_H

Expert Member
Joined
Mar 31, 2008
Messages
2,864
Reaction score
12
Location
Cape Town - NS
I need a way to close all modal forms on my MDI application. Closing all mdiChild forms is easy but I can't figure out how to close any/all open Modal forms? I know that the user is supposed to close the Modal forms but my software is privilage based and I want it to "lock" the interface after x minutes of inactivity.

Is there some way of getting a list of active modal forms like when using Form.MDIChildren? eg.:

for I := frmMain.MDIChildCount - 1 downto 0 do
Begin
frmMain.MDIChildren.Close;
End;

The only way I can think of doing it up to now is to use Window Messages but this means having to add code to every modal form where as I want to be able to do it from only the main form.
 
I suggest creating a pointer array of type TForms *

Then at initialization assign manually each of the forms to the array;

When you want to search each of the Forms just loop through the array
 
Hi Chris,

You can loop through Application.Components (using Application.ComponentCount) and test
if (Application.Components[0].ClassType = TForm) (or similar)

Hope that helps.

Cheers.
 
Hi Chris,

You can loop through Application.Components (using Application.ComponentCount) and test
if (Application.Components[0].ClassType = TForm) (or similar)

Hope that helps.

Cheers.

Ah, this might work, I thought of frmMain.Components but obviously that wont work... will give it a try :D
 
Array ftw...

OK, Application.Components only returns the main form...

What I've done is as Simoski advised and created an Array of TForm. I populate the array with the forms and when the timer triggers I loop through the array and close the forms from last in array to first so as to not get "no owner' errors. This is more work though as I have to put extra code in every procedure that opens a new modal form.
 
Woops, found a shorter way lol... changed the owner of the modal forms from self to application and now it is listed under the application.components... MUCH SHORTER... thanks for the input guys
 
Top
Sign up to the MyBroadband newsletter
X