Author: Pieter Valentijn
Getting A master Detail Relation in a component on a way it's easely streamed.
Using a TStream Class Decendant
Answer:
The collection Class is one of my favorit when it comes to storing multiple Data
with one component its even posible to include the Collection in its own item
making it useful for recursion (The Collection Can have a item withs can hold a
other collection.
If you want the standard Editor for Collections u have to use a TOwnedCollection i
think its ijn the unit Classes from delphi 4 but if u have D3 u need to Make that
class first like this
1 TOwnedCollection = class(TCollection)
2 private3 FOwner: TPersistent;
4 protected5 function GetOwner: TPersistent; override;
6 public7 // Fil in the AOwner in The Fowner proeprty on the Create Constructor .8 constructor Create(AOwner: TPersistent; ItemClass: TCollectionItemClass);
9 end;
10 11 //Heres a Example Of a collection That does that 12 13 unit Unit1;
14 15 interface16 17 uses18 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
19 StdCtrls;
20 21 type22 TMyCollection = class(TOwnedCollection)
23 24 end;
25 26 TMyCollectionItem = class(TCollectionItem)
27 private28 FANummer: Integer;
29 FAString: string;
30 FMoreCollections: TMyCollection;
31 procedure SetANummer(const Value: Integer);
32 procedure SetAString(const Value: string);
33 procedure SetMoreCollections(const Value: TMyCollection);
34 public35 constructor Create(Collection: TCollection); override;
36 destructor Destroy; override;
37 published38 property AString: stringread FAString write SetAString;
39 property ANummer: Integer read FANummer write SetANummer;
40 property MoreCollections: TMyCollection read FMoreCollections write41 SetMoreCollections;
42 end;
43 44 TCollectionWrapper = class(TComponent)
45 46 private47 FCollection: TMyCollection;
48 procedure SetCollection(const Value: TMyCollection);
49 public50 constructor Create(AOwner: TComponent); override;
51 destructor Destroy; override;
52 53 published54 property Collection: TMyCollection read FCollection write SetCollection;
55 end;
56 57 implementation58 59 { TMyCollectionItem }60 61 constructor TMyCollectionItem.Create(Collection: TCollection);
62 begin63 inherited;
64 FMoreCollections := TMyCollection.Create(self, TMyCollectionItem);
65 end;
66 67 destructor TMyCollectionItem.Destroy;
68 begin69 FMoreCollections.free;
70 inherited;
71 72 end;
73 74 procedure TMyCollectionItem.SetANummer(const Value: Integer);
75 begin76 FANummer := Value;
77 end;
78 79 procedure TMyCollectionItem.SetAString(const Value: string);
80 begin81 FAString := Value;
82 end;
83 84 procedure TMyCollectionItem.SetMoreCollections(const Value: TMyCollection);
85 begin86 FMoreCollections := Value;
87 end;
88 89 { TCollectionWrapper }90 91 constructor TCollectionWrapper.Create(AOwner: TComponent);
92 begin93 inherited;
94 FCollection := TMyCollection.Create(self, TMyCollectionItem);
95 end;
96 97 destructor TCollectionWrapper.Destroy;
98 begin99 FCollection.free;
100 inherited;
101 102 end;
103 104 procedure TCollectionWrapper.SetCollection(const Value: TMyCollection);
105 begin106 FCollection := Value;
107 end;
108 109 end.
110 111 This is how you could addres it Run time
112 113 procedure TForm1.Button1Click(Sender: TObject);
114 var115 ACollection: TCollectionWrapper;
116 begin117 ACollection := TCollectionWrapper.Create(Self);
118 try119 // Default add gives u a TCollectionItem So u need to cast it120 with TMyCollectionItem(ACollection.Collection.Add()) do121 begin122 AString := 'Hallo';
123 ANummer := 5;
124 MoreCollections.add;
125 126 end;
127 128 finally129 ACollection.Free;
130 end;
131 end;
If you register this component u will be able to use the default Collection Editor
Design time
Component Download: http://www.xs4all.nl/~suusie/Pieter/Programs/CollectionComponent.ziphttp://www.xs4all.nl/~suusie/Pieter/Programs/CollectionComponent.zip