2005. március 13., vasárnap
TCollection The Class for Master Detail Relations
Problem/Question/Abstract:
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 will be able to use the default Collection Editor Design time
Component Download: http://www.xs4all.nl/~suusie/Pieter/Programs/CollectionComponent.zip
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése