Guiate de esta unidad tiene lo basico pa crear las colecciones
Código Delphi
[-]
unit Prueba;
interface
uses
Classes;
type
TMiComponente = class;
TMiItem = class(TCollectionItem)
private
FAlignment: TAlignment;
protected
function GetMiComponente: TMiComponente;
function GetDisplayName: string; override;
public
procedure Assign(Source: TPersistent); override;
property MiComponente: TMiComponente read GetMiComponente;
published
property Alignment: TAlignment read FAlignment write FAlignment default taLeftJustify;
end;
TMiItemClass = class of TMiItem;
TMiLista = class(TCollection)
private
FMiComponente: TMiComponente;
function GetMiItem(Index: Integer): TMiItem;
procedure SetMiItem(Index: Integer; Value: TMiItem);
protected
function GetOwner: TPersistent; override;
procedure Update(Item: TCollectionItem); override;
public
constructor Create(MiComponente: TMiComponente;
InfoListClass: TMiItemClass);
function Add: TMiItem;
property MiComponente: TMiComponente read FMiComponente;
property Items[Index: Integer]: TMiItem read GetMiItem write SetMiItem; default;
end;
TMiComponente = class(TComponent)
private
FMiLista: TMiLista;
procedure SetMiLista(const Value: TMiLista);
protected
function CreateMiLista: TMiLista; dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MiLista: TMiLista read FMiLista write SetMiLista;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Ejemplos', [TMiComponente]);
end;
constructor TMiComponente.Create(AOwner: TComponent);
begin
inherited;
FMiLista := CreateMiLista;
end;
function TMiComponente.CreateMiLista: TMiLista;
begin
Result := TMiLista.Create(Self, TMiItem);
end;
destructor TMiComponente.Destroy;
begin
FMiLista.Free;
inherited;
end;
procedure TMiComponente.SetMiLista(const Value: TMiLista);
begin
FMiLista := Value;
end;
procedure TMiItem.Assign(Source: TPersistent);
begin
if Source is TMiItem then
begin
if Assigned(Collection) then
Collection.BeginUpdate;
try
FAlignment := TMiItem(Source).Alignment;
finally
if Assigned(Collection) then
Collection.EndUpdate;
end;
end else
inherited Assign(Source);
end;
function TMiItem.GetDisplayName: string;
begin
Result := inherited GetDisplayName;
end;
function TMiItem.GetMiComponente: TMiComponente;
begin
if Assigned(Collection) and (Collection is TMiLista) then
Result := TMiLista(Collection).MiComponente
else
Result := nil;
end;
function TMiLista.Add: TMiItem;
begin
Result := TMiItem(inherited Add);
end;
constructor TMiLista.Create(MiComponente: TMiComponente;
InfoListClass: TMiItemClass);
begin
inherited Create(InfoListClass);
FMiComponente := MiComponente;
end;
function TMiLista.GetMiItem(Index: Integer): TMiItem;
begin
Result := TMiItem(inherited Items[Index]);
end;
function TMiLista.GetOwner: TPersistent;
begin
Result := FMiComponente;
end;
procedure TMiLista.SetMiItem(Index: Integer; Value: TMiItem);
begin
Items[Index].Assign(Value);
end;
procedure TMiLista.Update(Item: TCollectionItem);
begin
inherited;
end;
end.