Search code examples
delphidelphi-xe8

How to get all components (Tlabel,Tedit ...) in a form in Delphi?


I successfully coded a runtime event that changes the size of my TLabel when I re-size the form

procedure TForm3.pack(Sender: TObject);
begin
    Label1.Font.Size:=Floor(50*(Form3.Width/Screen.Width)*(Form3.Height/Screen.Height));
end; 

Now I want to get an array of all the components on my form, so I loop on and re-size them.

Any help please, if there is a predefined option or procedure thanks to tell me about it (like responsive)

Thanks


Solution

  • You can use the form's Controls property. Since all controls inherit from TControl, and TControl has a Font property, it makes it pretty easy (although the property is protected in TControl, so you'll need an interposer class):

    type
      TCtrl = class(TControl);
    
    var
      i: Integer;
      NewSize: Integer;
    begin
      NewSize := Floor(50*(Form3.Width/Screen.Width)*(Form3.Height/Screen.Height));
      for i := 0 to ControlCount - 1 do
        TCtrl(Controls[i]).Font.Size := NewSize;
    end;
    

    Note that some controls (such as TPanel and TTabSheet) can parent other controls, so they'll have their own Controls list. You'll need to loop through those as well.