我有以下组件,其中包含从自定义类派生的属性:
unit MyComponentTest3;
interface
uses Windows, ExtCtrls,Classes,Controls;
type
TMyClass3 = class
myString: string;
myNumber: double;
end;
TMyComponentTest3 = class(TCustomPanel)
private
FMyProperty: TMyClass3;
procedure SetMyProperty(Value: TMyClass3);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetMyPropertyPublic(AmyString: string; AmyNumber: double);
published
property MyProperty: TMyClass3 read FMyProperty write SetMyProperty;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TMyComponentTest3]);
end;
{ TMyComponentTest3 }
constructor TMyComponentTest3.Create(AOwner: TComponent);
begin
inherited;
FMyProperty:= TMyClass3.Create;
end;
destructor TMyComponentTest3.Destroy;
begin
FMyProperty.Free;
inherited;
end;
procedure TMyComponentTest3.SetMyProperty(Value: TMyClass3);
begin
with FMyProperty do
begin
myString:= Value.myString;
myNumber:= Value.myNumber;
end;
end;
procedure TMyComponentTest3.SetMyPropertyPublic(AmyString: string;
AmyNumber: double);
var
auxMyProperty: TMyClass3;
begin
auxMyProperty:= TMyClass3.Create;
with auxMyProperty do
begin
myString:= AmyString;
myNumber:= AmyNumber;
end;
SetMyProperty(auxMyProperty);
auxMyProperty.Free;
end;
end.
它工作正常,但是当我在设计时设置属性的值并运行程序时,值会消失。如果我设置组件的值,保存所有内容,当我加载值时,我得到的问题不再存在......
我想这个问题是因为我一直在创建自定义类的实例,但我不确定,而且我不知道如何解决这个问题。有谁知道我该怎么做?
答案 0 :(得分:4)
TMyClass3
需要从TPersistent
派生才能在DFM中流式传输。它还应该可选地覆盖虚拟Assign()
方法,SetMyProperty()
然后可以调用该方法。
试试这个:
unit MyComponentTest3;
interface
uses
Windows, ExtCtrls, Classes, Controls;
type
TMyClass3 = class(TPersistent)
private
FMyString: string;
FMyNumber: double;
public
procedure Assign(Source: TPersistent); override;
published
property myString: string read FMyString write FMyString;
property myNumber: double read FMyNumber write FMyNumber;
end;
TMyComponentTest3 = class(TCustomPanel)
private
FMyProperty: TMyClass3;
procedure SetMyProperty(Value: TMyClass3);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetMyPropertyPublic(AmyString: string; AmyNumber: double);
published
property MyProperty: TMyClass3 read FMyProperty write SetMyProperty;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TMyComponentTest3]);
end;
{ TMyClass3 }
procedure TMyClass3.Assign(Source: TPersistent);
begin
if Source is TMyClass3 then
begin
with TMyClass3(Source) do
begin
Self.FMyString := myString;
Self.FMyNumber := myNumber;
end;
end else
inherited;
end;
{ TMyComponentTest3 }
constructor TMyComponentTest3.Create(AOwner: TComponent);
begin
inherited;
FMyProperty := TMyClass3.Create;
end;
destructor TMyComponentTest3.Destroy;
begin
FMyProperty.Free;
inherited;
end;
procedure TMyComponentTest3.SetMyProperty(Value: TMyClass3);
begin
FMyProperty.Assign(Value);
end;
procedure TMyComponentTest3.SetMyPropertyPublic(AmyString: string; AmyNumber: double);
begin
with MyProperty do
begin
myString := AmyString;
myNumber := AmyNumber;
end;
end;
end.
答案 1 :(得分:0)
constructor TMyComponentTest3.Create(AOwner: TComponent);
begin
inherited;
FMyProperty := TMyClass3.Create;
FMyProperty.SetSubComponent(True);
end;