表格是否已经公开?

Jlo*_*uro 6 delphi

我一直在使用以下代码来检查表单是否已存在:

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;
Run Code Online (Sandbox Code Playgroud)

几年前我从参与的项目中得到了它.这是我第一个Delphi项目之一.

有用.

但是如果有更好,更快的方法,我本周就会徘徊.

Ken*_*ite 12

您可以使用Screen.Forms代替.它减少了您正在迭代的项目:

function FormExists(apForm: TForm): boolean;
var 
  i: Word;
begin
  Result := False;
  for i := 0 to Screen.FormCount - 1 do
    if Screen.Forms[i] = apForm then 
    begin
      Result := True;
      Break;
    end;
end;
Run Code Online (Sandbox Code Playgroud)

但是,值得注意的是,如果你apForm已经存在,你知道它存在并且没有必要进行搜索.