5

我试图弄清楚如何通过它来搜索标签Caption

for I := ComponentCount - 1 downto 0 do
begin
  if Components[i] is TLabel then
    if Components[i].Caption = mnNumber then
    begin
      Components[i].Left := Left;
      Components[i].Top := Top + 8;
    end;
end;

我收到一个错误:Undeclared identifier: 'Caption'
我该如何解决这个问题?

4

5 回答 5

13

迭代Components[]是错误的方法。这只会产生表单拥有的组件。您将错过任何动态添加的、不属于表单的组件,或属于框架的组件。

相反,您应该使用Controls[]. 但是,这只会产生第一代孩子。如果有更深的父/子嵌套,那么您需要递归。那是更多的工作。我使用一些助手来简化它。我已经将它们包装在这个单元中:

unit ControlEnumerator;

interface

uses
  System.SysUtils, System.Generics.Collections, Vcl.Controls;

type
  TControls = class
  private
    type
      TEnumerator<T: TControl> = record
        FControls: TArray<T>;
        FIndex: Integer;
        procedure Initialise(WinControl: TWinControl; Predicate: TFunc<T, Boolean>);
        class function Count(WinControl: TWinControl; Predicate: TFunc<T, Boolean>): Integer; static;
        function GetCurrent: T;
        function MoveNext: Boolean;
        property Current: T read GetCurrent;
      end;
      TEnumeratorFactory<T: TControl> = record
        FWinControl: TWinControl;
        FPredicate: TFunc<T, Boolean>;
        function Count: Integer;
        function Controls: TArray<T>;
        function GetEnumerator: TEnumerator<T>;
      end;
  public
    class procedure WalkControls<T: TControl>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>; Method: TProc<T>); static;
    class function Enumerator<T: TControl>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>=nil): TEnumeratorFactory<T>; static;
    class function ChildCount<T: TControl>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>=nil): Integer; static;
  end;

implementation

{ TControls.TEnumerator<T> }

procedure TControls.TEnumerator<T>.Initialise(WinControl: TWinControl; Predicate: TFunc<T, Boolean>);
var
  List: TList<T>;
  Method: TProc<T>;
begin
  List := TObjectList<T>.Create(False);
  Try
    Method :=
      procedure(Control: T)
      begin
        List.Add(Control);
      end;
    WalkControls<T>(WinControl, Predicate, Method);
    FControls := List.ToArray;
  Finally
    List.Free;
  End;
  FIndex := -1;
end;

class function TControls.TEnumerator<T>.Count(WinControl: TWinControl; Predicate: TFunc<T, Boolean>): Integer;
var
  Count: Integer;
  Method: TProc<T>;
begin
  Method :=
    procedure(Control: T)
    begin
      inc(Count);
    end;
  Count := 0;
  WalkControls<T>(WinControl, Predicate, Method);
  Result := Count;
end;

function TControls.TEnumerator<T>.GetCurrent: T;
begin
  Result := FControls[FIndex];
end;

function TControls.TEnumerator<T>.MoveNext: Boolean;
begin
  inc(FIndex);
  Result := FIndex<Length(FControls);
end;

{ TControls.TEnumeratorFactory<T> }

function TControls.TEnumeratorFactory<T>.Count: Integer;
begin
  Result := TEnumerator<T>.Count(FWinControl, FPredicate);
end;

function TControls.TEnumeratorFactory<T>.Controls: TArray<T>;
var
  Enumerator: TEnumerator<T>;
begin
  Enumerator.Initialise(FWinControl, FPredicate);
  Result := Enumerator.FControls;
end;

function TControls.TEnumeratorFactory<T>.GetEnumerator: TEnumerator<T>;
begin
  Result.Initialise(FWinControl, FPredicate);
end;

class procedure TControls.WalkControls<T>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>; Method: TProc<T>);
var
  i: Integer;
  Control: TControl;
  Include: Boolean;
begin
  if not Assigned(WinControl) then begin
    exit;
  end;
  for i := 0 to WinControl.ControlCount-1 do begin
    Control := WinControl.Controls[i];
    if not (Control is T) then begin
      Include := False;
    end else if Assigned(Predicate) and not Predicate(Control) then begin
      Include := False;
    end else begin
      Include := True;
    end;
    if Include then begin
      Method(Control);
    end;
    if Control is TWinControl then begin
      WalkControls(TWinControl(Control), Predicate, Method);
    end;
  end;
end;

class function TControls.Enumerator<T>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>): TEnumeratorFactory<T>;
begin
  Result.FWinControl := WinControl;
  Result.FPredicate := Predicate;
end;

class function TControls.ChildCount<T>(WinControl: TWinControl; Predicate: TFunc<T, Boolean>): Integer;
begin
  Result := Enumerator<T>(WinControl, Predicate).Count;
end;

end.

现在你可以像这样解决你的问题:

var
  lbl: TLabel;
....
for lbl in TControls.Enumerator<TLabel>(Form) do
  if lbl.caption=mnNumber then
  begin
    lbl.Left := Left;
    lbl.Top := Top + 8;
  end;

或者您可以使用谓词将标题测试放在迭代器中:

var
  Predicate: TControlPredicate;
  lbl: TLabel;
....
Predicate := function(lbl: TLabel): Boolean
  begin
    Result := lbl.Caption='hello';
  end;
for lbl in TControls.Enumerator<TLabel>(Form, Predicate) do
begin
  lbl.Left := Left;
  lbl.Top := Top + 8;
end;
于 2013-02-15T12:01:12.497 回答
10

最后一条信息出现在您对 Golez 回答的评论中:您的标签是在运行时创建的,因此他们有可能没有Form作为所有者。您将需要使用Controls[]数组来查看表单所包含的所有控件,并递归查看所有TWinControl后代,因为它们也可能包含TLabel's。

如果您要执行此分配并针对不同类型的控件,您可能需要实现某种帮助程序,这样您就不会经常重复自己。看看大卫对现成解决方案的回答,除了解决手头的问题之外,它还设法包括一些“花里胡哨”;就像使用匿名函数来操作找到的控件的能力一样,它可以使用匿名函数根据任何条件过滤控件。

在开始使用如此复杂的解决方案之前,您可能应该了解最简单的解决方案。一个非常简单的递归函数,它简单地查看TControls从表单开始的所有容器。像这样的东西:

procedure TForm1.Button1Click(Sender: TObject);

  procedure RecursiveSearchForLabels(const P: TWinControl);
  var i:Integer;
  begin
    for i:=0 to P.ControlCount-1 do
      if P.Controls[i] is TWinControl then
        RecursiveSearchForLabels(TWinControl(P.Controls[i]))
      else if P.Controls[i] is TLabel then
        TLabel(P.Controls[i]).Caption := 'Test';
  end;

begin
  RecursiveSearchForLables(Self);
end;

使用 David 的通用代码,上面的代码可以重写为:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TControls.WalkControls<TLabel>(Self, nil,
    procedure(lbl: TLabel)
    begin
      lbl.Caption := 'Test';
    end
  );
end;
于 2013-02-15T12:41:16.510 回答
2

ComponentCount 仅用于计数。使用 Components 数组查找实际组件。为方便起见,您可以将标签放在 TLabel 变量中,这也将允许您使用在 TComponent 中不可见的标签特定属性。您也可以使用with它,但我认为它会降低可读性。

var
  l: TLabel;


for I := ComponentCount -1 downto 0 do
begin
  if Components[i] is TLabel then // Check if it is.
  begin
    l := TLabel(Components[i]); // Typecast, to reach it's properties.
    if l.Caption = mnNumber then
    begin
      l.Left := Left;
      l.Top := Top +8;
    end;
  end;
end;
于 2013-02-15T10:38:35.247 回答
1

编译器不知道您的 Components[i] 是 TLabel。

您需要像这样将组件转换为 Tlabel:

for I := ComponentCount - 1 downto 0 do
begin
  if Components[i] is TLabel then //here you check if it is a tlabel
    if TLabel(Components[i]).Caption = mnNumber then //and here you explicitly tell the
    begin                                            //compiler to treat components[i]
      TLabel(Components[i]).Left := Left;            //as a tlabel.
      TLabel(Components[i]).Top := Top + 8;         
    end;
end;

这是必需的,因为 components[i] 不知道标题。

于 2013-02-15T14:01:00.890 回答
0

尝试这个:

  对于 I := ControlCount-1 下降到 0 做
  开始
    if Controls[i] is TLabel then // 检查是否是。
    开始
      if (Controls[i] as TLabel).Caption = mnNumber then
      开始
        (Controls[i] as TLabel).Left := Left;
        (Controls[i] as TLabel).Top := Top +8;
      结尾;
    结尾;
  结尾;
于 2013-02-15T11:21:21.563 回答