unit Collec1;
interface
// Note: TCollection and TCollectionItem are defined in Classes.Pas.
uses Classes;
type
TMyComponent = class;
TMyCollectionItem = class(TCollectionItem)
private
FText: string;
FMoreStuff: LongInt;
function GetDisplayName: string; override;
procedure SetText(const Value: string);
procedure SetMoreStuff(const Value: LongInt);
public
published
property Text: string read FText write SetText;
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('Sample', [TMyComponent]);
end;
{ TMyCollectionItem }
// Note: Inherited default behavior of GetDisplayName is to
// return the classname.
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;
end.
{--------------------------------------------------------------------}
Notes
-----
In this minimal example we didn't override the Assign method for
the TCollectionItem, but this method should have further support.
Here's an example of how you might implement Assign in the above
project:
procedure TMyCollectionItem.Assign(Source: TPersistent);
begin
if Source is TMyCollectionItem then
begin
Text := TMyCollectionItem(Source).Text;
MoreStuff := TMyCollectionItem(Source).MoreStuff;
Exit;
end;
inherited Assign(Source);
end;
给分哦.