我想使用自定義類的數組作爲我的組件的屬性,但問題是值未保存到組件,這意味着如果我設置值,保存所有工作,再次打開該項目,該值組件將消失...我的代碼如下所示:作爲屬性的自定義類的數組
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;
...
。哪一個更好?或者還有其他更好的想法嗎?
我測試了這個版本,它工作正常,我只需要在我的真實代碼中測試,這有點複雜,謝謝 – Felipe
正在尋找相同的東西。來自Remy的很好回答,非常感謝。 –