我要TCollection及TCollectionItem的详细用法(比如ADD等)(50分)

  • 主题发起人 主题发起人 孤月独明
  • 开始时间 开始时间

孤月独明

Unregistered / Unconfirmed
GUEST, unregistred user!
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;
给分哦.
 
我要的是一个例子,我要把一些值写到TCollection里去。比如:
var
l:TCollection;
begin
增加 id=1;m='wo'
id=2;m='ni'
id=3;m='ta'
 
我以前用了两年vb5,vb的集合用起来很简单,对象加入集合方便的紧.
delphi的集合(Tcollect)必须一堆对象配合使用.
我知道的自己定义集合来管理对象必须按上面的方法.
使用时:
var myitems:TmyCollect'
myitem:Tmycollectitem;
begin
myItems:=Tmycollect.create(nil)
myitem:=myItems.add;
myitem.name:='navy';
myitem.sex:='m';
myitems.free;
end;

得到item项
myitems[0].name-->navy

 
to navy:
TMyCollection要安装到Sample去么?也就是编写一个TMyCollection组件??
 
同志,你作为一个类文件加入project,用就好了,不用安装嘛,动态生成对象.
 
接受答案了.
 
后退
顶部