Author: Tomas Rutkauskas I would like to create a component that contains a TCollection with TCollectionItems. Then I want to contain another TCollection with TCollectionItems within the TCollectionItems. I am trying to create a collection of sections, which would contain a collection of items for each section. Answer: It's not difficult to implement such functionality. One thing you need to care about is the valid Owner for your collections, presumably, the main control would be the best choice. Below is an example of such a component: 1 { ... } 2 type 3 TYourCollectionItem = class; 4 TYourCollection = class; 5 TColControl = class; 6 7 TYourCollectionItem = class(TCollectionItem) 8 protected 9 FFirstString: string; 10 FChildCollection: TYourCollection; 11 procedure SetIndex(Value: Integer); override; 12 function GetDisplayName: string; override; 13 public 14 constructor Create(Collection: TCollection); override; 15 destructor Destroy; override; 16 procedure Assign(Source: TPersistent); override; 17 published 18 property FirstString: string read FFirstString write FFirstString; 19 property ChildCollection: TYourCollection read FChildCollection write 20 FChildCollection; 21 end; 22 23 TYourCollection = class(TOwnedCollection) 24 protected 25 function GetItem(Index: Integer): TYourCollectionItem; 26 procedure SetItem(Index: Integer; Value: TYourCollectionItem); 27 public 28 constructor Create(AOwner: TPersistent); 29 property Items[Index: Integer]: TYourCollectionItem read GetItem write SetItem; 30 end; 31 32 TColControl = class(TComponent) 33 protected 34 FCollection: TYourCollection; 35 public 36 constructor Create(AOwner: TComponent); override; 37 destructor Destroy; override; 38 published 39 property Collection: TYourCollection read FCollection write FCollection; 40 end; 41 42 { ... } 43 44 constructor TYourCollectionItem.Create(Collection: TCollection); 45 begin 46 inherited Create(Collection); 47 FChildCollection := TYourCollection.Create(Collection.Owner); 48 end; 49 50 destructor TYourCollectionItem.Destroy; 51 begin 52 FChildCollection.Free; 53 inherited Destroy; 54 end; 55 56 procedure TYourCollectionItem.SetIndex(Value: Integer); 57 begin 58 inherited SetIndex(Value); 59 ShowMessage(IntToStr(Value)); 60 end; 61 62 function TYourCollectionItem.GetDisplayName: string; 63 begin 64 Result := FFirstString; 65 end; 66 67 procedure TYourCollectionItem.Assign(Source: TPersistent); 68 begin 69 FFirstString := TYourCollectionItem(Source).FFirstString; 70 FChildCollection.Assign(TYourCollectionItem(Source).ChildCollection); 71 end; 72 73 constructor TYourCollection.Create(AOwner: TPersistent); 74 begin 75 inherited Create(AOwner, TYourCollectionItem); 76 end; 77 78 function TYourCollection.GetItem(Index: Integer): TYourCollectionItem; 79 begin 80 Result := TYourCollectionItem(inherited GetItem(Index)); 81 end; 82 83 procedure TYourCollection.SetItem(Index: Integer; Value: TYourCollectionItem); 84 begin 85 inherited SetItem(Index, Value); 86 end; 87 88 constructor TColControl.Create(AOwner: TComponent); 89 begin 90 inherited Create(AOwner); 91 FCollection := TYourCollection.Create(Self); 92 end; 93 94 destructor TColControl.Destroy; 95 begin 96 FCollection.Free; 97 FCollection := nil; 98 inherited Destroy; 99 end;