德尔福,按名称查找表格

Mlo*_*y87 2 forms delphi

我如何通过名字找到表格?在这个表格上我有编辑(TEdit),我想在这个TEdit(它的名字,例如:地址)中写一些东西,但我只有表格名称.你能帮助我吗?

Fre*_*ing 8

有一种更简单的方法可以按名称查找表单.由于所有自动创建的表单对象都归Application对象所有并且TApplication继承自TComponent,因此您可以通过Application.Components数组属性或使用Application.FindComponent方法进行迭代.

var 
  Form: TForm;
begin
  Form := Application.FindComponent('LostForm1') as TForm;
  if Assigned(Form) then
    Form.Show
  else
    { error, can't find it } 
Run Code Online (Sandbox Code Playgroud)

请注意,FindComponent不区分大小写.


小智 6

This answer assumes you are making a VCL application. I don't know if FireMonkey has a similar solution.

All forms are added to the global Screen (declared in Vcl.Forms) object when they are created. Thus you can make a little helper function like this

function FindFormByName(const AName: string): TForm;
var
  i: Integer;
begin
  for i := 0 to Screen.FormCount - 1 do
  begin
    Result := Screen.Forms[i];
    if (Result.Name = AName) then
      Exit;
  end;
  Result := nil;
end;
Run Code Online (Sandbox Code Playgroud)