Update a default value only during design-time

喜欢而已 提交于 2019-12-20 07:42:59

问题


I would like to update the default value of a private variable linked to a public property only during design-time, in case it's possible.

TMyComp = class(TComponent)
  private
    FColumnWidth: Integer;
    FColumnWidthDef: Integer;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property ColumnWidth: Integer read FColumnWidth write SetColumnWidth  default 50;
  end;

...

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;

  FColumnWidth:= 50;
  FColumnWidthDef:= FColumnWidth;
end;

destructor TMyComponent.Destroy;
begin
  FColumnWidth:= 0;
  FColumnWidthDef:= 0;

  inherited;
end;

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

    FColumnWidthDef:= FColumnWidth; //<-- how to run this only during design-time?
  end;
end;

What I would like to do is to store in a private variable the default value for the property ColumnWidth. Inside of run-time code of the component there is a reset button that should change the property to default value FColumnWidthDef. If I do it like the code from above, this value will be updated in design-time and also in run-time.


回答1:


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

but this do not go to dfm file and when you run app your def will be gone why not to put this as another published property? or better write "stored" function like it is done many times in delphi source code like this

property BorderIcons: TBorderIcons read FBorderIcons write SetBorderIcons stored IsForm
      default [biSystemMenu, biMinimize, biMaximize];


来源:https://stackoverflow.com/questions/36762153/update-a-default-value-only-during-design-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!