We help IT Professionals succeed at work.

Using TFont in TComponent

zebada
zebada asked
on
I have a component derived from TGraphicControl and one of the published properties is of type TFont like this:

    FGridFont: TFont;
    procedure SetGridFont(const Value: TFont);
    property GridFont: TFont read FGridFont write SetGridFont;

procedure Txxx.SetGridFont(const Value: TFont);
begin
  FGridFont.Assign(Value);
  Invalidate;
end;

FGridFont is created in the component's constructor and freed in the destructor.

When I change the font using the ellipses in the object inspector the Invalidate is invoked and my component is repainted with the new font settings.

If, however, I expand the TFont properties in the object inspector and change individual font settings then the Invalidate is NOT invoked and my component is not repainted.

How can I get it to repaint in the second scenario?

Paul
Comment
Watch Question

Commented:
you can use TFont.OnChange:

type
  Txxx = class(TGraphicControl)
  private
    FGridFont: TFont;
    procedure FontChange(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

constructor Txxx.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FGridFont := TFont.Create;
  ...
  FGridFont.OnChange := FontChange;
end;

destructor Txxx.Destroy;
begin
  ...
  FGridFont.OnChange := nil;
  FGridFont.Free;
  inherited Destroy;
end;

procedure Txxx.FontChange(Sender: TObject);
begin
  Invalidate;
end;

HTH
TOndrej

Explore More ContentExplore courses, solutions, and other research materials related to this topic.