I have found that the best way to do this is to ask the form if it is open. You can do this with the CLASS procedure / function. It is safe to call a class procedure / form function, even if it does not exist.
add a class function to your public desclaration form.
type TForm2 = class(TForm) ... private { Private declarations } ... public { Public declarations } class function FormExists: Boolean; end; class function TForm2.FormExists: Boolean; var F: TForm2; I: Integer; begin F := nil; for i := Screen.FormCount - 1 DownTo 0 do if (Screen.Forms[i].Name = 'Form2') then begin F := Screen.Forms[I] As TForm2; break; end; Result := F <> nil; end;
So, from any unit that has the form 2 in the uses clause, you can call
if Form2.FormExists then ...
source share