如何获取一个组件的初始值属性?
How to get the initial value of a component property?
让我们举一个组件的例子,其中包含一个名为 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
的初始值?
值 100 在组件内部不可用,因为它存储在组件所在的窗体、框架或数据模块的 DFM 资源中。 las,稍后再次从 DFM 读取该值可能会变得乏味(尽管可能)。因此,您最好在 FormCreate
事件期间将值保存在表单字段中,稍后使用它将该组件 属性 重置为保存的值。
让我们举一个组件的例子,其中包含一个名为 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
的初始值?
值 100 在组件内部不可用,因为它存储在组件所在的窗体、框架或数据模块的 DFM 资源中。 las,稍后再次从 DFM 读取该值可能会变得乏味(尽管可能)。因此,您最好在 FormCreate
事件期间将值保存在表单字段中,稍后使用它将该组件 属性 重置为保存的值。