How to change the font for all elements of elements that have the Font property?
4 answers
For all inheritors from TWinControl Font property can be inherited from the parent container. The ParentFont property is responsible for this. Therefore, if you change the Font of a form, it will also change for all elements nested in this form with ParentFont = True (and then cascade from container to content).
UPD: Enumerate all components of the form with the filter of those classes in which you need to change the font:
for i := 0 to Form1.Components - 1 do begin if Form1.Components[i] is TButton then TButton(Form1.Components[i]).Font := MyFont else if Form1.Components[i] is TLabel then TLabel(Form1.Components[i]).Font := MyFont else // ... и так далее ... end; - This does not suit me, because on the contrary I have disabled this property, since it prevents me from the main program! - delphikettle
- Nepoymu, why turn it off if there is a direct need to use it? From bicycles, I can suggest doing a recursive descent on all nested containers. Or the usual linear cycle for all components of the form. - KiTE
- Updated the answer with the source code that will go through all the components of the form of the necessary types - KiTE
|
for i := 0 to Form1.ControlCount - 1 do case Form1.Controls[i].ClassName of "TLabel", "TButton" { ... типы имеющие свойство font }: (TLabel)(Form1.Controls[i]).Font = NewFont; { может быть некоторые элементы также нужно рекурсивно перебирать } end; something like this, with an enumeration of all controls. I can allow syntax errors.
- An option for the particular case when all elements are located on the top level of the form. That is, there are no nested containers. For example: if the TButton lies in the TPanel, and that in turn is on the form, the TPanel will fall into the cycle, but the TButton will not. - KiTE
- I have a mistake for some reason! The compiler puts the cursor on of - delphikettle
- Enclosed containers recursion bust. I don’t know how on
Delphi, inVBAI did it, there all the elements, including nested containers, are moved to thecontrolsform. - Ildar - I am a kettle, and I do not understand how it is! - delphikettle
- What do not you understand? What is recursion or what are
Controls? Comma corrected. - Ildar
|
for Delphi it will look like this:
type
TMyControl = class (TControl)
public
property Font;
end; procedure SetFontByComponent (comp: TComponent; font: string);
var
i: Integer;
ctrl: TControl;
begin
for i: = 0 to comp.ComponentCount-1 do
if comp.Components [i] is TControl then
(TMyControl (comp.Components [i])). Font.Name: = font;
end;
procedure TForm1.btn1Click (Sender: TObject);
begin
SetFontByComponent (self, 'Verdana');
end;
|
I solve this problem in C ++ Builder like this, including for TLabel, etc., including all child objects:
- Create your own class to get public access to the Font property.
class TMyControl: public TControl
{
public:
__property Font;
};
- Next, we iterate over all the components except the top menu.
AnsiString Name ("Verdana");
for (int i = 0; iComponentCount; ++ i)
{
TControl * ctrl = dynamic_cast <TControl *> (this-> Components [i]);
if (NULL == ctrl) continue;
((TMyControl *) ctrl) -> Font-> Name = Name;
};
- Question about Delphi. What does it have to do with ++? - Kromster
- I published both versions for both Delphi and C ++ Builder. Because the system is the same, only the language is different, and very often Delphi developers are looking for something, but they only find it in C ++ and are forced to translate it into Pascal and vice versa. For newbies, this can be difficult, which is why I posted both options at once; both Delphi developers and C ++ Builder developers can search. You are not asking why there are several examples of the same code for the same platform in different languages on msdn? or ask? - has
- ps .: and also because I write exclusively in c ++ and immediately wrote the answer first on C ++ (this is better than not giving any), but then I spent some time and prepared another version on pascal. The code is tested both there and there. - has
- I plyusanul option. But let's not turn the answers into a gallery of languages, otherwise we will have to write another 15-20 languages. The question clearly indicated - Delphi. - Kromster
- And let's not. You expressed your opinion with your minus and plus, I expressed my own with the code. Since he himself has repeatedly encountered similar difficulties, I consider my position more correct. Behind this whole flood, I propose to close and do business - has
|