怎样创建一个组件,组件含有一个类型是组件的属性(100分)

  • 主题发起人 主题发起人 txiuq
  • 开始时间 开始时间
T

txiuq

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样创建一个组件,组件里含有一个数组属性items,而该属性类型是一个组件,
items的组件个数是动态创建的。
如:
TAComponet.x:integer;
TAComponet.str:string;
TAComponet.items[index:integer]:TOotherComponet;
items 能够读写。最好给出例子。
 
可能你是想设计一个对象数组属性
下面是取自VCL源代码例子
TDBGridColumns = class(TCollection)
private
FGrid: TCustomDBGrid;
function GetColumn(Index: Integer): TColumn;
function InternalAdd: TColumn;
procedure SetColumn(Index: Integer; Value: TColumn);
procedure SetState(NewState: TDBGridColumnsState);
function GetState: TDBGridColumnsState;
protected
function GetOwner: TPersistent; override;
procedure Update(Item: TCollectionItem); override;
public
constructor Create(Grid: TCustomDBGrid; ColumnClass: TColumnClass);
function Add: TColumn;
procedure LoadFromFile(const Filename: string);
procedure LoadFromStream(S: TStream);
procedure RestoreDefaults;
procedure RebuildColumns;
procedure SaveToFile(const Filename: string);
procedure SaveToStream(S: TStream);
property State: TDBGridColumnsState read GetState write SetState;
property Grid: TCustomDBGrid read FGrid;
property Items[Index: Integer]: TColumn read GetColumn write
SetColumn; default;
end;
注意:
必须写两个私有方法
function GetColumn(Index: Integer): TColumn;
procedure SetColumn(Index: Integer; Value: TColumn);
然后:
property Items[Index: Integer]: TColumn read GetColumn write
SetColumn; default;
对应换上你要的

 
unit AComponent;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TAComponent = class(TComponent)
private
{ Private declarations }
FItems:array of TComponent;
procedure SetItem(index:integer;Value:TComponent);
function GetItem(index:integer):TComponent;
procedure SetCount(Value:integer);
function GetCount:integer;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AComponent:TComponent);override;
property Count:integer read GetCount write SetCount;
property Items[index:integer]:TComponent read GetItem write SetItem;
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TAComponent]);
end;

{ TAComponent }

constructor TAComponent.Create(AComponent: TComponent);
begin
inherited;

FItems:=nil;
SetCount(10);
end;

function TAComponent.GetCount: integer;
begin
Result:=High(FItems);
end;

function TAComponent.GetItem(index: integer): TComponent;
begin
if (index>High(FItems)) or (index<0) then
raise Exception.Create('Error Index');
Result:=FItems[index];
end;

procedure TAComponent.SetCount(Value: integer);
var
i:integer;
FCount:integer;
begin
if Value<0 then raise Exception.Create('Error Value');
FCount:=High(FItems)+1;
if Value<>0 then SetLength(FItems,Value) else FItems:=nil;
if Value>FCount then
for i:=FCount to High(FItems) do FItems:=nil;
end;

procedure TAComponent.SetItem(index: integer; Value: TComponent);
begin
if (index>High(FItems)) or (index<0) then
raise Exception.Create('Error Index');
FItems[index]:=Value;
end;

end.
 
多人接受答案了。
 
后退
顶部