Overview
When writing components, I like to assign my published properties to their default values, making the Object Inspector display all newly changed properties in Bold, which, of course, is very useful for everyone who uses the component, since they can easily identify between standard and modified values.
Example
Here is an example of a component that contains two Color and Two Font properties:
I make published default properties inside the class structure:
type TMyComponent = class(TComponent) private FColor: TColor; FColorTo: TColor; FFont: TFont; FFontHot: TFont; procedure SetColor(const Value: TColor); procedure SetColorTo(const Value: TColor); procedure SetFont(const Value: TFont); procedure SetFontHot(const Value: TFont); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Color: TColor read FColor write SetColor default clGreen; property ColorTo: TColor read FColorTo write SetColorTo default clBlue; property Font: TFont read FFont write SetFont;
and constructor:
constructor TMyComponent.Create(AOwner: TComponent); begin inherited Create(AOwner); FColor := clGreen; FColorTo := clBlue; FFont := TFont.Create; FFont.Color := clRed; FFont.Name := 'Segoe UI'; FFont.Size := 10; FFont.Style := []; FFontHot:= TFont.Create; FFontHot.Color := clNavy; FFontHot.Name := 'Verdana'; FFontHot.Size := 8; FFontHot.Style := [fsItalic]; end; destructor TMyComponent.Destroy; begin FFont.Free; FFontHot.Free; inherited Destroy; end;
Problem
The problem I am facing is how to make the published font property “flagged” by default?
When I add a component to the form, the TFont properties in the Object Inspector are shown in bold (not the default). If I clicked an ellipse next to each published font, I see that the values I set from the constructor are displayed, but again they are highlighted in bold. Oddly enough, the size value for each font is never displayed in bold (i.e., displayed unchanged).
Standard published properties can make default very easy, but how can you do this with classes like TFont that have sub-properties?
I see no way to set default values from the class structure, only in the constructor. Even if I could determine the default values from the class structure (which seems impossible), I doubt it will work because the font has not been created yet. For example (which obviously does not work):
published property FontHot: TFont read FFontHot write SetFontHot default FFontHot.Color := clRed; //< set default?
So, how can I publish the published TFont property property as the default and unchanged when adding a new component to the Object Inspector?
I suppose the same applies to other classes similar to TFont, which as sub-properties.