如何有条件地将属性保存到 DFM? [英] How to conditionally make a property save to the DFM or not?
问题描述
我有一对组件,其中一个组件通过设置属性附加"到另一个组件.比如……
I have a pair of components, one of the components gets "attached" to the other by setting a property. For example...
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
.
So in the object inspector for TSub
, user would would choose the TMain
to be associated with it.
在子组件中,我有一个属性 Value
带有 getter 和 setter.如果子组件的值设置为 0
,则 getter 会从它所附加到的 TMain
中获取 Default
属性..
In the sub component, I have a property Value
with both a getter and setter. In the event that the sub component's value is set to 0
, the getter then obtains the Default
property from the TMain
that it's attached to...
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
值.
That makes the object inspector (and thus the property its self) return the default value from the main rather than the set 0
value.
我想做的是确保当 TSub
组件的属性保存到 DFM 时,如果它是 0
(因此使用 main 中的默认值代替).目前,在保存 DFM 后,来自 main 的默认值的任何值都将保存在 sub 的值中,这不是我想要的.
What I would like to do is make sure that when the TSub
component's properties get saved to the DFM, that it doesn't save this property if it's 0
(thus using the default from the main instead). Currently, after saving the DFM, whatever value came from the default of the main will get saved in the value for the sub, which is not what I want.
自然地,一个属性会被标记为 default 0;
例如表示如果该属性的值设置为 0
,那么该属性将不会被保存到DFM.但由于默认值可能会有所不同,因此我无法为该属性标记默认值(因为它需要定义默认值).
Naturally, a property would be marked as default 0;
for example indicating that if the property's value is set to 0
, then that property wouldn't get saved into the DFM. But since the default might vary, there's no way I can flag a default value for this property (because it expects the default value to be defined).
如果 TSub
组件已设置为 0
而是使用默认值,我如何构造 不 将此属性保存到 DFM从属性 getter 中的 main 开始?
How can I structure the TSub
component to not save this property to the DFM if it's been set to 0
and instead using the default from the main in the property getter?
推荐答案
property Value: Integer read GetValue write SetValue stored IsValueStored;
在哪里
function TSub.IsValueStored: Boolean;
begin
Result := (FValue <> 0) or (FMain = nil);
end;
如果我猜对了.
这篇关于如何有条件地将属性保存到 DFM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!