組件數組

在Delphi中,早期的時候咱們沒辦法創建 像這樣的 array of TButton 的組件數組。咱們的使用的方法或許是:數組

利用 Form  的Components屬性,這個屬性就是Form中全部的組件的數組,經過它來索引組件,顯然這是不方數據結構

便的:spa

var
  Index : Integer;
begin
  For Index:=0 to ControlCount-1 do
  begin
    if Components[Index] is TLinkLabel then
    begin
      (Components[Index] As TLinkLabel).Caption:='hello';
    end
  end;
end;

顯然這樣仍是有侷限性的。code

在DelphiXE 中咱們會發現 TComponentList 這樣的數據結構,就是經過它咱們即可創建一個組件鏈表,它的父類orm

是TObjectList,這個也能夠作組件鏈表。blog

var
  ComList : TComponentList;
begin
  ComList := TComponentList.Create;
  ComList.Add(lbl1);
  ComList.Add(lbl2);
  ComList.Add(lbl3);
  (ComList.Items[0] as TLabel).Caption := 'Hello';
end;
相關文章
相關標籤/搜索