I have been using the following code to check if a form already exists:
function FormExists(apForm: TObject): boolean;
var i: Word;
begin
Result := False;
for i := 0 to Application.ComponentCount-1 do
if Application.Components[i] = apForm then begin
Result := True;
Break;
end;
end;
I got it some years ago from a project I participated in. It was one of my first Delphi projects.
It works.
But this week I got wandering if there is a better, faster way to do this.
You can use Screen.Forms for that instead. It lessens the items you’re iterating through:
However, it’s worth noting that if you have
apFormalready, you know it exists and there’s no need to be searching for it.