2016-05-16 19 views
0

讓我們舉一個包含名爲ColumnWidth的屬性的組件的例子,其默認值爲50.在設計時,我將該值更改爲100,然後編譯該應用程序。如何獲取組件屬性的初始值?

現在,我想在我的組件內部實現一個Reset to default(彈出式菜單)按鈕,它將初始化ColumnWidth爲100,以防用戶在同一時間更改它。

TMyComponent = class(TComponent) 
    private 
    FVirtualStringTree: TVirtualStringTree; 
    FColumnWidth: Integer; 
    FColumnWidthDef: Integer; 
    public 
    constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
    procedure ResetToDefault; 
    published 
    property ColumnWidth: Integer read FColumnWidth write SetColumnWidth default 50; 
    property VirtualStringTree: TVirtualStringTree read FVirtualStringTree write FVirtualStringTree; 
    end; 

constructor TMyComponent.Create(AOwner: TComponent); 
begin 
    inherited; 
    FColumnWidth:= 50; 
end; 

destructor TMyComponent.Destroy; 
begin 
    inherited; 
end; 

procedure TMyComponent.SetColumnWidth(const Value: Integer); 
begin 
    if FColumnWidth <> Value then FColumnWidth:= Value; 
end; 

procedure TMyComponent.ResetToDefault; 
begin 
    ColumnWidth:= FColumnWidthDef; 
end; 

在組件方法下,如何存儲初始值ColumnWidth

+1

爲什麼不到處使用常量? –

+0

幾個星期前,你還沒有回答同樣的問題,你還想加入@RobKennedy的評論工作嗎? –

+0

對於之前的「類似」問題,我想到了同樣的想法,但從我的錯誤。我認爲我需要從設計時間開始,但實際上有必要在實時進行。現在我認爲這個問題更加清楚。編程是一種愛好,而不是一份工作,所以互聯網和Stackoverflow是我唯一的培訓師。如果有時我會提出愚蠢的問題,謝謝你的理解。 – REALSOFO

回答

1

值100在組件內部不可用,因爲它存儲在組件駐留的窗體,框架或數據模塊的DFM資源中。唉,從DFM中再次讀取這個值可能會很乏味(儘管可能)。因此,您最好在FormCreate事件期間將值保存在表單字段中,稍後使用它將該組件屬性重置爲保存的值。

+0

我相信'FColumnWidthDef'就是出於這個目的。 –