How to check if a modal form exists?
Posted on 2004-10-27
I am trying to write a centralized (in a seperate unit) procedure that manages the creation, display and release of forms. The slimmed down version of my procedure looks roughtly like this:
procedure OpenAForm(Sender: TObject);
// check if frmMain exists
if frmMain <> nil then
if Sender = frmMain.mnuAbout then f := TfrmAbout.Create(frmMain);
if Sender = frmMain.mnuProgramSettings then f := TfrmProgramSettings.Create(frmMain);
if Sender = frmMain.mnuFSAdvancedSetup then f:= TfrmFSAdvancedSetup.Create(frmMain);
// check if frmFSAdvancedSetup exists
if frmFSAdvancedSetup <> nil then
if Sender = frmFSAdvancedSetup.cmdManagef := TfrmFSManagement.Create(frmFSAdvancedSetup);
The way the forms are shown on screen is as follows: frmMain displays modal form frmFSAdvancedSetup, which in turn displays a modal form frmFSManagement. Checking the existance of the main form goes fine, but even if frmFSAdvancedSetup is on screen, it returns nil, thereby crashing my app with an Access Violation.
So, how exactly can I make sure that modal forms are detected as being onscreen. This procedure looked fine in theory, but reality turned out a bit less succesful. Any help appreciated.
-edit: slimmed the code down a bit too much, corrected-