【发布时间】:2013-05-25 00:41:21
【问题描述】:
我正在尝试使用自定义类的数组作为我的组件的属性,但问题是这些值没有保存到组件中,这意味着如果我设置了值,请保存所有内容并再次打开项目中,组件的值消失了...我的代码如下所示:
unit Unit1;
interface
uses Windows, ExtCtrls,Classes,Controls;
type
TMyClass=class(TPersistent)
private
FName: string;
FValue: double;
public
property Name: string read FName write FName;
property Value: double read FValue write FValue;
end;
TMyComponent= class(TCustomPanel)
private
FMyArray: array[0..200] of TMyClass;
function GetmyArray(Index: Integer): TMyClass;
procedure SetMyArray(index: Integer; Value: TMyClass);
public
property myArray[index: Integer]: TMyClass read GetMyArray write SetMyArray;
end;
implementation
function TMyComponent.GetmyArray(Index: Integer): TMyClass;
begin
result:= FmyArray[Index];
end;
procedure TMyComponent.SetMyArray(index: Integer; Value: TMyClass);
begin
FMyArray[index].FName:= Value.FName;
FMyArray[index].FValue:= Value.FValue;
end;
end.
我知道只能流式传输已发布的属性,但问题是我的属性是一个数组,无法发布...
我的建议是使用DefineProperties() 提供自定义流,但我不知道如何使用数组来做到这一点。
我认为的另一种可能性是将 TMyClass 修改为一种 TMyComponent 可能是它的父类的类,就像在 TChart 中所做的那样,您可以向其中添加不同的系列类。但我不知道这应该是什么类
TMyClass=class(T???????????)
这样我就可以取出属性 MyArray 并创建 TMyClass 并添加到 TMyComponent 中,如下所示:
MyArray1.parent:= MyComponent1;
MyArray2.parent:= MyComponent2;
...
。哪一个是更好的选择?或者还有什么更好的办法吗?
【问题讨论】:
标签: class delphi components