Delphi - Center a label in a form at a certain height


Very good, I have the following problem:

I'm trying to create a simple runtime form where TLabel is displayed, and I need them to be centered and at a certain height each. I have been able to center them correctly using TLabel.Align := alClient and TLabel.Alignment := taCenter , but I can not assign the height I need, because TLabel.Layout only lets me specify above, center or below.

How can I assign the TLabel a height value such as ClientHeight div 3 ?

I have this code to create a label:

with TLabel.Create(Self) do
  Align := alClient;
  Alignment := taCenter;
  Top := (F.ClientHeight div 3) * 2;
  caption:= 'Texto de la etiqueta';
  Font.Size:= 18;
  Font.Color := RGB(128, 0, 0);
  Parent:= F;

It seems that when applying Align and Alignment ignores what I specified in Top , then I do not know how to give the desired value.


I am using Lazarus v1.6RC1 in Windows 10 . The application is also for Windows 10 .

Thanks in advance.

asked by Daniel Rivers 08.02.2016 в 16:22

2 answers


The problem is that if you are using as alignment for the TLabel.Align := alClient component, this will cause the component to adjust automatically (for its width and height) to occupy all the available space of the component that contains it.

If so, the width and height properties can no longer be assigned manually as you comment ( label1.width := ClientHeight div 3 ), because they will be ignored.

You will have to change the alignment property ( TLabel.Align ) and then you can manually assign the width and height.

ADDED: In the latest versions of delphi, there is a component called TGriPanelLayout that facilitates the positioning of several components in a form. Maybe in Lazarus there is a similar one.

answered by 08.02.2016 / 17:31

Sometimes it is interesting not to use Align or aligment ... without daring the anchor properties of each component, in case the form is resized where they are ...

I do not know if this can be worth ...



answered by 10.05.2018 в 07:37