如何获取组件属性的初始值?

时间:2016-05-16 12:04:23

标签: delphi components

让我们举一个包含名为ColumnWidth的属性的组件的示例,默认值为50.在设计时,我将值更改为100,然后编译应用程序。

现在,我想在我的组件中实现一个Reset to default(弹出菜单)按钮,将ColumnWidth初始化为值100,以防用户同时更改它。

TMyComponent = class(TComponent)
  private
    FVirtualStringTree: TVirtualStringTree;
    FColumnWidth: Integer;
    FColumnWidthDef: Integer;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure ResetToDefault;
  published
    property ColumnWidth: Integer read FColumnWidth write SetColumnWidth  default 50;
    property VirtualStringTree: TVirtualStringTree read FVirtualStringTree  write FVirtualStringTree; 
  end;

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  FColumnWidth:= 50;
end;

destructor TMyComponent.Destroy;
begin
  inherited;
end;

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

procedure TMyComponent.ResetToDefault;
begin
  ColumnWidth:= FColumnWidthDef;
end;

在组件方法下,如何存储ColumnWidth的初始值?

1 个答案:

答案 0 :(得分:1)

值100在组件内部不可用,因为它存储在组件所在的表单,框架或数据模块的DFM资源中。唉,稍后再次从DFM读取该值可能会很繁琐(尽管可能)。因此,您最好在FormCreate事件期间将值保存在表单字段中,稍后再使用它将该组件属性重置为保存的值。

相关问题