unit ColTest;
interface
// Note: TCollection and TCollectionItem are defined in Classes.Pas.
uses Classes, Graphics;
type
TMyComponent = class;
TMyCollectionItem = class(TCollectionItem)
private
FText: string;
FListColorMot : TStringList;
FColorFont : TFont;
FMoreStuff: LongInt;
function GetDisplayName: string; override;
procedure SetText(const Value: string);
procedure SetMoreStuff(const Value: LongInt);
Procedure writeListColorMot(lst : TStringList);
Procedure WriteColorFont(value : TFont);
public
constructor Create(Collection: TCollection); virtual;
destructor Destroy; override;
published
property Text: string read FText write SetText;
property ListColorMot : TStringList read FListColorMot write writeListColorMot;
property ColorFont : TFont read FColorFont write WriteColorFont;
property MoreStuff: LongInt read FMoreStuff write SetMoreStuff;
end;
TMyCollection = class(TCollection)
private
FMyComponent: TMyComponent;
function GetItem(Index: Integer): TMyCollectionItem;
procedure SetItem(Index: Integer; Value: TMyCollectionItem);
protected
function GetOwner: TPersistent; override;
public
constructor Create(MyComponent: TMyComponent);
function Add: TMyCollectionItem;
property Items[Index: Integer]: TMyCollectionItem
read GetItem write SetItem; default;
end;
TMyComponent = class(TComponent)
private
FItems: TMyCollection;
procedure SetItems(Value: TMyCollection);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Items: TMyCollection read FItems write SetItems;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMyComponent]);
end;
{ TMyCollectionItem }
// Note: Inherited default behavior of GetDisplayName is to
// return the classname.
constructor TMyCollectionItem.Create(Collection: TCollection);
begin
SetCollection(Collection);
FListColorMot := TStringList.Create;
FColorFont := TFont.Create;
end;
destructor TMyCollectionItem.Destroy;
begin
FListColorMot.Clear;
FListColorMot.Free;
FColorFont.Free;
SetCollection(nil);
inherited Destroy;
end;
function TMyCollectionItem.GetDisplayName: string;
begin
Result := Text;
if Result = '' then Result := inherited GetDisplayName;
end;
procedure TMyCollectionItem.SetText(const Value: string);
begin
if FText <> Value then
FText := Value;
end;
procedure TMyCollectionItem.SetMoreStuff(const Value: LongInt);
begin
if FMoreStuff <> Value then
FMoreStuff:= Value;
end;
{ TMyCollection }
constructor TMyCollection.Create(MyComponent: TMyComponent);
begin
inherited Create(TMyCollectionItem);
FMyComponent := MyComponent;
end;
function TMyCollection.Add: TMyCollectionItem;
begin
Result := TMyCollectionItem(inherited Add);
end;
function TMyCollection.GetItem(Index: Integer): TMyCollectionItem;
begin
Result := TMyCollectionItem(inherited GetItem(Index));
end;
procedure TMyCollection.SetItem(Index: Integer;
Value: TMyCollectionItem);
begin
inherited SetItem(Index, Value);
end;
// Note: You must override GetOwner in Delphi 3.x to get
// correct streaming behavior.
function TMyCollection.GetOwner: TPersistent;
begin
Result := FMyComponent;
end;
{ TMyComponent }
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FItems := TMyCollection.Create(Self);
end;
destructor TMyComponent.Destroy;
begin
FItems.Free;
inherited Destroy;
end;
procedure TMyComponent.SetItems(Value: TMyCollection);
begin
FItems.Assign(Value);
end;
Procedure TMyCollectionItem.WriteColorFont(value : TFont);
begin
if Assigned(FColorFont) then FColorFont.Assign(value);
end;
Procedure TMyCollectionItem.writeListColorMot(lst : TStringList);
begin
FListColorMot.Assign(lst);
end;
end.
Partager