简体   繁体   English

Delphi XE2:如何在设计时设置属性?

[英]Delphi XE2: How to make properties settable at design time?

I have a simple component我有一个简单的组件

type
  TTimedScrollBox = class(TScrollBox)
  private
    procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
  protected
    FSkipTime: Cardinal;
    FEndTimeout: Cardinal;
    FSkipScrollTimer: TTimer;
    FEndScrollTimer: TTimer;
    FLastMessage: TWMVScroll;
    FWaiting: boolean;
    FLastMessageValid: boolean;
    FLog: TStrings;
    FSkipCount: integer;
    procedure SkipTimerEvent(Sender: TObject);
    procedure EndTimerEvent(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Log: TStrings read FLog;
  published
    property ScrollSkipTime: Cardinal read FSkipTime write FSkipTime default 100;
    property ScrollEndTimeout: Cardinal read FEndTimeout write FEndTimeout default 200;
  end;

I want to be able to specify values for ScrollSkipTime and ScrollEndTimeout at design time.我希望能够在设计时为 ScrollSkipTime 和 ScrollEndTimeout 指定值。 I had the impression that all I need to do this is write the code as shown, but我的印象是我需要做的就是编写如图所示的代码,但是

  1. The default values I provided don't appear in the designer and我提供的默认值不会出现在设计器中
  2. When I set a breakpoint in the constructor and see what it's doing for an instance, the values of the fields behind the property are 0 even though the values I entered in the designer are stored in the DFM.当我在构造函数中设置断点并查看它为实例执行的操作时,即使我在设计器中输入的值存储在 DFM 中,属性后面的字段的值为 0。

What am I missing/doing wrong?我错过了什么/做错了什么?

The default values you provide in the property declaration are only used by the streaming framework and the IDE.您在属性声明中提供的默认值仅由流框架和 IDE 使用。 For instance, the property is not streamed if the value is equal to the default value.例如,如果该值等于默认值,则不会流式传输该属性。 The default values are also used to allow the IDE to highlight, in bold, which values have been modified from their defaults.默认值还用于允许 IDE 以粗体突出显示已从默认值修改的值。

What is missing in your code is that you need to set the values of the backing field in your component's constructor.您的代码中缺少的是您需要在组件的构造函数中设置支持字段的值。 We can't see that code but I'm pretty sure that is what is missing.我们看不到那个代码,但我很确定那是缺少的东西。

This issue is covered in the documentation:文档中涵盖了此问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM