仅在设计时更新默认值 [英] Update a default value only during design-time

查看:112
本文介绍了仅在设计时更新默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我想在设计阶段更新链接到公共属性的私有变量的默认值。 code> TMyComp = class(TComponent)
private
FColumnWidth:Integer;
FColumnWidthDef:整数;
protected
public
构造函数Create(AOwner:TComponent);覆盖
析构函数覆盖
发布
属性ColumnWidth:整数读FColumnWidth写SetColumnWidth默认为50;
结束

...

构造器TMyComponent.Create(AOwner:TComponent);
开始
继承;

FColumnWidth:= 50;
FColumnWidthDef:= FColumnWidth;
结束

析构函数TMyComponent.Destroy;
begin
FColumnWidth:= 0;
FColumnWidthDef:= 0;

继承;
结束

procedure TMyComponent.SetColumnWidth(const Value:Integer);
begin
如果FColumnWidth<>值然后
begin
FColumnWidth:= Value;

FColumnWidthDef:= FColumnWidth; //< - 如何仅在设计时运行?
结束
结束

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

解决方案

  procedure TMyComponent.SetColumnWidth(const Value:Integer); 
begin
如果FColumnWidth<>值然后
begin
FColumnWidth:= Value;
如果csDesigning在ComponentState然后
FColumnWidthDef:= FColumnWidth;
结束
结束

但这不会去dfm文件,当你运行应用程序你的def将消失
为什么不把这作为另一个出版物?
或更好的写存储功能像在这样的delphi源代码这样做很多次这样

 属性BorderIcons: TBorderIcons读取FBorderIcons写入SetBorderIcons存储IsForm 
default [biSystemMenu,biMinimize,biMaximize];


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.

解决方案

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];

这篇关于仅在设计时更新默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆