仅在设计时

时间:2016-04-21 07:07:50

标签: delphi components

我想在设计时间内更新链接到公共属性的私有变量的默认值,以防万一。

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;

我想要做的是在私有变量中存储属性ColumnWidth的默认值。在组件的运行时代码内部有一个重置按钮,应该将属性更改为默认值FColumnWidthDef。如果我像上面的代码一样,这个值将在设计时和运行时更新。

1 个答案:

答案 0 :(得分:3)

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

但这不会转到dfm文件,当你运行app时你的def将会消失 为什么不把它作为另一个出版物? 或者更好地编写“存储”函数,就像在这样的delphi源代码中多次完成

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