加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何有条件地将属性保存到DFM?

发布时间:2020-12-15 09:20:55 所属栏目:大数据 来源:网络整理
导读:我有一对组件,其中一个组件通过设置属性“附加”到另一个组件.例如… type TMain = class(TComponent) ... published property Default: Integer read FDefault write SetDefault; end; TSub = class(TComponent) ... published property Value: Integer rea
我有一对组件,其中一个组件通过设置属性“附加”到另一个组件.例如…

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.

在子组件中,我有一个带有getter和setter的属性Value.如果子组件的值设置为0,则getter将从附加到的TMain获取Default属性…

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;

这使得对象检查器(以及其自身的属性)从main而不是set 0值返回默认值.

我想要做的是确保当TSub组件的属性保存到DFM时,如果它为0则不保存此属性(因此使用默认值来取代).目前,在保存DFM之后,来自main的默认值的任何值都将保存在sub的值中,这不是我想要的.

当然,属性将被标记为默认值0;例如,指示如果属性的值设置为0,则该属性不会保存到DFM中.但由于默认值可能会有所不同,因此我无法标记此属性的默认值(因为它需要定义默认值).

如果将此属性设置为0并使用属性getter中main的默认值,如何构造TSub组件以不将此属性保存到DFM?

解决方法

property Value: Integer read GetValue write SetValue stored IsValueStored;

哪里

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

如果我做对了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读