Examples Delphi

Title: TCollection The Class for Master Detail Relations
Question: 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
TOwnedCollection = class(TCollection)
private
FOwner: TPersistent;
protected
function GetOwner: TPersistent; override;
public
// Fil in the AOwner in The Fowner proeprty on the Create Constructor .
constructor Create(AOwner: TPersistent; ItemClass: TCollectionItemClass);
end;
Heres a Example Of a collection That does that
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyCollection = Class (TOwnedCollection)
end;
TMyCollectionItem = Class (TCollectionItem)
private
FANummer: Integer;
FAString: String;
FMoreCollections: TMyCollection;
procedure SetANummer(const Value: Integer);
procedure SetAString(const Value: String);
procedure SetMoreCollections(const Value: TMyCollection);
Public
constructor Create(Collection: TCollection); Override ;
Destructor Destroy ; Override;
Published
Property AString : String read FAString write SetAString;
Property ANummer : Integer read FANummer write SetANummer;
Property MoreCollections : TMyCollection read FMoreCollections write SetMoreCollections;
end;
TCollectionWrapper = Class(TComponent)
private
FCollection: TMyCollection;
procedure SetCollection(const Value: TMyCollection);
Public
Constructor Create(AOwner : TComponent); Override ;
Destructor Destroy ; Override ;
Published
Property Collection : TMyCollection read FCollection write SetCollection;
end;
implementation
{ TMyCollectionItem }
constructor TMyCollectionItem.Create(Collection: TCollection);
begin
inherited;
FMoreCollections := TMyCollection.Create(self,TMyCollectionItem);
end;
destructor TMyCollectionItem.Destroy;
begin
FMoreCollections.free ;
inherited;
end;
procedure TMyCollectionItem.SetANummer(const Value: Integer);
begin
FANummer := Value;
end;
procedure TMyCollectionItem.SetAString(const Value: String);
begin
FAString := Value;
end;
procedure TMyCollectionItem.SetMoreCollections(const Value: TMyCollection);
begin
FMoreCollections := Value;
end;
{ TCollectionWrapper }
constructor TCollectionWrapper.Create(AOwner: TComponent);
begin
inherited;
FCollection := TMyCollection.Create(self,TMyCollectionItem);
end;
destructor TCollectionWrapper.Destroy;
begin
FCollection.free ;
inherited;
end;
procedure TCollectionWrapper.SetCollection(const Value: TMyCollection);
begin
FCollection := Value;
end;
end.
This is how you could addres it Run time
procedure TForm1.Button1Click(Sender: TObject);
var
ACollection : TCollectionWrapper ;
begin
ACollection := TCollectionWrapper.Create(Self) ;
Try
// Default add gives u a TCollectionItem So u need to cast it
With TMyCollectionItem(ACollection.Collection.Add()) do
begin
AString := 'Hallo' ;
ANummer := 5 ;
MoreCollections.add ;
end;
Finally
ACollection.Free ;
end;
end;
If you register this component u wil be able to use the default Collection Editor Design time