سؤال

Delphi XE2

I have the followind custom component:

  TNaharWebDBEdit = class(TUniDBEdit)
  private
    FThemeColor : TNaharPalleteColor;
    FThemeController: TNaharThemeController;
    procedure   OnObserver(Sender: TObject; AParam, AValue: Variant);
    procedure   UpdateColor;
    procedure   SetColor(const Value: TNaharPalleteColor);
    function    GetThemeManager: TNaharThemeManager;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
  published
    property    ThemeColor: TNaharPalleteColor read FThemeColor write SetColor default npcWhite;
    property    ThemeManager: TNaharThemeManager read GetThemeManager;
  end;

the SetColor code is:

procedure TNaharWebDBEdit.SetColor(const Value: TNaharPalleteColor);
begin
  FThemeColor := Value;

  UpdateColor;
end;

where UpdateColor just retrive the current assigned color for the theme and set is on Self.Color.

Everything works fine. The property ThemeColor shows up on the property view and I can change it to other values. However right after I save the form and open back again it is back to the original value, and not even the default one.

I made 12 components like this, and all similiar code. Only on the Panel (from TuniPanel) is working correctly, the property keeps the value assigned during the design and retrieve it correctly on runtime.

Somewhow the value is not saved or not property restored. How to catch that?

EDIT:

  TNaharPalleteColor = (npcMainColor, npcColorA, npcColorB, npcColorC, npcColorD, npcWhite, npcBlack, npcUnknown, npcNone);

I have changed the value of ThemeColor to npcWhite and saved. I opened the DFM on Notepad++ and found it was NOT SAVED

I have added the STORED TRUE on the property and it did not make any difference, still not storing it there.

However if I close the form and open it again it shows the ThemeColor as npcMainColor (0) instead of the default that is npcWhite

EDIT: I have opened other forms that are using this component and could see that previously the property was getting saved. When opened and saved back again it gets removed from the DFM. The only one that remains working is the descendant from TuniPanel, that is a TPanel. But it has exacly the same code, it is all copy and paste.

I have removed this line from the class:

    FThemeController: TNaharThemeController;

and the component is now saving correctly the FThemeColor on the DFM, working as expected. Why?

هل كانت مفيدة؟

المحلول

See the documentation for Default Property Values. The default value is not saved to the .dfm. I guess you forgot to initialize your FThemeColor to the default value in the constructor. (Since instance fields are always initialized to zero values, this is only necessary if the default value is non-zero.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top