2013-05-27 69 views
0

我有以下的成分包含從自定義類衍生的特性:,不會出現在運行時

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

任何理由不做出'TMyClass3'記錄,而不是一類? –

+0

這只是一個簡單的例子,在我的真實代碼中它更復雜... – Felipe

+2

@J ...不管它有多複雜,關鍵的決定因素是你是否想要值或引用賦值語義。 –

回答

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. 
+0

沒有...似乎它沒有達到這個分配過程,這些值在運行時仍然沒有出現,我在分配過程中放了一條消息來檢查,並且它從不顯示它 – Felipe

+1

'Assign()'不是將被調用,因爲在DFM流式傳輸過程中不調用屬性設置器。這個建議只是爲了清理你的二傳手並實施更好的封裝。你的問題是另外一回事,但你仍然需要從基礎開始 - 從「TPersistent」派生出來,使得子值本身成爲發佈的屬性等等。你描述的這些值表明這些值並沒有被保存到DFM中,因此不會在稍後加載。 –

+0

感謝您的幫助,我會再次嘗試您的建議。 – Felipe

0
constructor TMyComponentTest3.Create(AOwner: TComponent); 
begin 
    inherited; 
    FMyProperty := TMyClass3.Create; 
    FMyProperty.SetSubComponent(True); 
end; 
相關問題