简体   繁体   中英

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. 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.

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. 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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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