重要的是要在控件A的constructor中创建B这个属性的实例
看下面的例子(另:可以参看TLabeledEdit)
unit object1;
interface
uses
Windows, Messages, SysUtils, Classes;
type
TTest = class(TComponent)
private
F1: string;
F2: string;
protected
public
published
{ Published declarations }
property P1: string read F1 write f1;
property P2: string read F2 write f2;
end;
Tobject1 = class(TComponent)
private
{ Private declarations }
F1: string;
F2: string;
FObj: TTest;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent);override ;
published
{ Published declarations }
property P1: string read F1 write f2;
property P2: string read F2 write f2;
property PObj: TTest read FObj;
end;
procedure Register;
implementation
constructor Tobject1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if Assigned(FObj) then exit;
FObj := TTest.Create(self);
FObj.Name := 'SubLabel';
SetSubComponent(True);
end;
procedure Register;
begin
RegisterComponents('test', [Tobject1]);
RegisterComponents('test', [TTest]);
end;
end.