TObjectList - como usar

Top  Previous  Next

uses Classes, Contnrs, SysUtils;

 

type

  TCampo = class(TObject)  // classe que será repetida n vezes

  private

    FX          : Integer;

    FY          : Integer;

    FSize       : Integer;

    FAlign      : Integer; // 0 - esquerda, 1 - direita e 2 - centro

    FVisivel    : Boolean; // se vai mostrar ou não

    FAlignChar  : string;  // 1º caracter dessa string é o caracter de alinhamento, por padrão #32

    FText       : string;  // conteúdo a ser impresso

    FFieldName  : string;  // usado na edição com preview

    FDescricao  : string;  // usado na edição com preview

  published

    property X        : Integer read FX         write FX;

    property Y        : Integer read FY         write FY;

    property Size     : Integer read FSize      write FSize;

    property Align    : Integer read FAlign     write FAlign;

    property AlignChar: string  read FAlignChar write FAlignChar;

    property Text     : string  read FText      write FText;

    property FieldName: string  read FFieldName write FFieldName;

    property Descricao: string  read FDescricao write FDescricao;

    property Visivel  : Boolean read FVisivel   write FVisivel;

  end;

 

type

  TCampoList = class(TObjectList)  // classe mãe

  private

    function GetCampo(Idx: Integer): TCampo;

    procedure SetCampo(Idx: Integer; const Value: TCampo);

  public

    property Items[Idx: Integer]: TCampo read GetCampo write SetCampo; default;

    function Add(ACampo: TCampo): Integer;

  end;

 

implementation  

  

function TCampoList.Add(ACampo: TCampo): Integer;

begin

  Result := inherited Add(ACampo);

end;

 

function TCampoList.GetCampo(Idx: Integer): TCampo;

begin

  Result := inherited Items[Idx] as TCampo;

end;

 

procedure TCampoList.SetCampo(Idx: Integer; const Value: TCampo);

begin

  inherited Items[Idx] := Value;

end;

 

 

------ uso:

 

var

  CampoList: TCampoList;

  I        : Integer;

begin

  CampoList := TCampoList.Create;

 

  for I := 1 to 10 do

  begin

    CampoList.Add(TCampo.Create);

    CampoList[I-1].Descricao := IntToStr(I);

  end;

 

  CampoList.Delete(1);

  CampoList.Delete(1);

 

  for I := 0 to CampoList.Count-1 do

    Memo1.Lines.Add(CampoList[I].Descricao);

 

  CampoList.Free;

end;

.