2013-12-13 21 views
4

我有一對組件,其中一個組件通過設置屬性被「附加」到另一個組件。例如...如何有條件地將屬性保存到DFM中?

type 
    TMain = class(TComponent) 
    ... 
    published 
    property Default: Integer read FDefault write SetDefault; 
    end; 

    TSub = class(TComponent) 
    ... 
    published 
    property Value: Integer read GetValue write SetValue; 
    property Main: TMain read FMain write SetMain; 
    end; 

所以在TSub的對象檢查,用戶將會選擇TMain與它有關。

在子組件中,我有一個屬性Value同時具有getter和setter。在該子組件的值被設定爲0的情況下,該吸氣劑則獲得來自,它的安裝於TMainDefault屬性...

function TSub.GetValue: Integer; 
begin 
    if FValue = 0 then begin 
    if Assigned(FMain) then begin 
     Result:= FMain.Default; 
    end else begin 
     Result:= 0; 
    end; 
    end else begin 
    Result:= FValue; 
    end; 
end; 

這使得對象檢查(以及因此的屬性自身)返回主值的默認值,而不是設置0的值。

我想要做的是確保當TSub組件的屬性保存到DFM時,如果它的0(因此使用默認值而不是main)來保存該屬性。目前,在保存DFM之後,無論從main的默認值中得到的值都將被保存在sub的值中,這不是我想要的值。

當然,一個屬性將被標記爲default 0;例如,指示如果該屬性的值設置爲0,那麼該屬性將不會保存到DFM中。但是由於默認值可能會有所不同,因此我無法標記此屬性的默認值(因爲它預計要定義默認值)。

我怎麼能結構TSub組件如果它使用從主默認的屬性的getter被設置爲0,而是這個屬性保存到DFM?

+0

http://docwiki.embarcadero.com/RADStudio/zh/Properties#Storage_Specifiers –

回答

7
property Value: Integer read GetValue write SetValue stored IsValueStored; 

其中

function TSub.IsValueStored: Boolean; 
begin 
    Result := (FValue <> 0) or (FMain = nil); 
end; 

,如果我得到它的權利。

+0

非常好,如此真棒,哇,將嘗試:-) –

+0

@Jerry - 謝謝。不要忘記檢查[documentation](http://docwiki.embarcadero.com/RADStudio/XE5/en/Properties#Storage_Specifiers)。 –

+0

完全像我需要的一樣,謝謝Sertac –

相關問題