M Schenkel
M Schenkel

Reputation: 6364

TFont Property to control font of subclassed controls

I have created a component descended from TPanel. In the constructor of the component I create several TButton components. I have created and surfaced a ButtonFont property of type TFont. This property controls the font of all the buttons on the component. Example:

TMyPanel = Class(TPanel)
private
    FButtonFont      : TFont;
    FExampleButton   : TButton;
    procedure SetButtonFont(Value: TFont);    
public
    property ButtonFont: TFont read FButtonFont write SetButtonFont;
    constructor Create (AOwner: TComponent); override;
end;        

constructor TMyPanel.Create (AOwner: TComponent);
begin
  FButtonFont := TFont.Create;
  FExampleButton := TButton.Create(self);
  FExampleButton.Parent := self;
  .......
  inherited;
end;

procedure TMyPanel.SetButtonFont(Value: TFont);

begin
  FButtonFont.Assign(Value);
  FExampleButton.Font := Value;
end;

The following will cause all subclassed buttons have their button font changed:

MyLabel.Font.Size := 22; 
MyPanel.ButtonFont := label1.font;

I can see the SetButtonFont method is being called.

How can I get something like this to cause all subclassed buttons to change their font size:

MyPanel.ButtonFont.Size := 22;

Upvotes: 0

Views: 354

Answers (1)

David Heffernan
David Heffernan

Reputation: 613382

Assign a handler to the font's OnChange event and update all the sub-controls' fonts in that handler:

constructor TMyPanel.Create(AOwner: TComponent);
begin
  inherited;
  FButtonFont := TFont.Create;
  FButtonFont.OnChange := ButtonFontChanged; // <-- here
  FExampleButton := TButton.Create(Self);
  FExampleButton.Parent := Self;
  ...
end;

destructor TMyPanel.Destroy;
begin
  ...
  FButtonFont.Free;
  inherited;
end;


procedure TMyPanel.ButtonFontChanged(Sender: TObject);
begin
  FExampleButton.Font := FButtonFont;
  ...
end;

Upvotes: 3

Related Questions