Recent

Author Topic: [SOLVED] How to update 'Font.Height' for all Controls of a TForm automatically?  (Read 320 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
The Main-Form of my program has about 30 GUI-Controls (Buttons, Labels, TComboBox, TSpinEditEx, etc). The user shall be able to change all their 'Font.Height' during runtime.

But when I set the new Height via 'Form1.Font.Height:=new_height' then a couple of GUI-Controls automatically switches to the new Height, but other GUI-Controls not. Because of property 'ParentFont=true' I would expect all GUI-Controls automatically to switch to the new Height. Then I found in the documentation: "If the Font of a control is changed by the application, then 'ParentFont' will be automatically set to False" and this applies, e.g. if I only change the font color, which is the reason, that some GUI-Controls automatically switch to the new Height and others not.

What would work is, to set the new Height manually to each individual GUI-Control like:
Code: Pascal  [Select][+][-]
  1. Form1.Button_Start.Height:=new_height;
  2. Form1.Label1.Height:=new_height;
  3. Form1.SpinEdit1.Height:=new_height;
  4. ...
but this would be a little uncomfortable...

So my question is: can I do this in a loop for all members of Form1.Components[]? If yes, for which "type" of members must I do this? Thanks in advance.
« Last Edit: November 15, 2025, 01:33:55 pm by Hartmut »

wp

  • Hero Member
  • *****
  • Posts: 13266
Re: How to update 'Font.Height' for all Controls of a TForm automatically?
« Reply #1 on: November 15, 2025, 11:47:37 am »
If you consequently have ParentFont of all controls set to true you only must change the Font of the form to propagate it to all controls on this form. See attached demo project (it shows that obviously TShellListView seems to ignore the form's Font settings - this should be reported).

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: How to update 'Font.Height' for all Controls of a TForm automatically?
« Reply #2 on: November 15, 2025, 12:05:36 pm »
Thank you wp for your post and your demo. Maybe we misunderstood:
 - I don't want to change the complete Font of all GUI-Controls of a Form - only 'Font.Height' should be changed
 - a couple of GUI-Controls has got 'ParentFont=false', because their Font.Color was individually changed.
 
It would work to set the new Height manually to each individual GUI-Control like:
Code: Pascal  [Select][+][-]
  1. Form1.Button_Start.Height:=new_height;
  2. Form1.Label1.Height:=new_height;
  3. Form1.SpinEdit1.Height:=new_height;
  4. ...
but this would be a little uncomfortable for 30 GUI-Controls...

So my question is: can I do this in a loop for all members of Form1.Components[]? If yes, for which "type" of members must I do this?  Which type of members do have a 'Font' property?

wp

  • Hero Member
  • *****
  • Posts: 13266
Re: How to update 'Font.Height' for all Controls of a TForm automatically?
« Reply #3 on: November 15, 2025, 12:25:00 pm »
So my question is: can I do this in a loop for all members of Form1.Components[]? If yes, for which "type" of members must I do this?  Which type of members do have a 'Font' property?
Most probably all controls on a form are owned by the form itself. This means that they are listed in the Components list of the form. Therefore you can iterate over all elements of Components to reach every control. Since the most primitive control, TControl, already has a public property Font you can change the font size after a type-cast.
Code: Pascal  [Select][+][-]
  1. // untested
  2. procedure SetFontSize(AForm: TForm; ANewSize: Integer);
  3. var
  4.   i: Integer;
  5. begin
  6.   for i := 0 to AForm.ComponentCount-1 do
  7.     if AForm.Components[i] is TControl then
  8.       TControl(AForm.Components[i]).Font.Size := ANewSize
  9.     else if AForm.Components[i] is TMenu then                // just as an example for non-TControl components (Does TMenu really have a Font?)
  10.       TMenu(AForm.Components[i]).Font.Size := ANewSize;
  11. end;

Maybe there are some other components with a font which do not descend from TControl. In this case, you must handle those by a separate type-cast, like TMenu in above example (but I think that TMenu does not have a Font at all, so this is a bad example...). There are also ways how to determine by means of RTTI whether a component has a specific published property, but I don't have the code at hand - search the forum for it.

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: How to update 'Font.Height' for all Controls of a TForm automatically?
« Reply #4 on: November 15, 2025, 01:33:36 pm »
That is exactly what I searched for. I implemented an tested it and it does exactly what I wanted.
TMenu does not have a 'Font' property, but I understand, what you wanted to demonstrate.

Thank you very much wp.

 

TinyPortal © 2005-2018