2 4 Votes

Delphi: Center the text on a label vertical and horizontal?

Question by Guest | 27/08/2012 at 12:45

I want to center the caption of a label vertically and horizontally, the text should therefore appear exactly in the middle, when I change the size of the label.

However, the text is always sticking at the top. But instead, the text should always be in the middle, just as it is with the panel. How can I achieve this? Unfortunately, I can not find a property for VerticalAlign with the label.

ReplyPositiveNegativeDateVotes

Delphian

Show Profile | Message
Avatar
8Best Answer
18 Votes

You just have to set the properties of "Alignment" and "Layout" in the Object Inspector or the code to centered. In the code, that is looking like this:

Label1.Alignment := taCenter;
Label1.Layout := tlCenter;

Alignment ensures the horizontal alignment, Layout provides the vertical alignment.

Under certain circumstances, you have also to set "AutoSize" to "False", in order to prevent the reset of the size of the label.
28/08/2012 at 20:20

ReplyPositive Negative
-1115 Votes

It dosn't work.

Working solution:

label1.Left:=
(label1.Width-label1.Width) div 2;

label1.top:=
(label1.Height-label1.Height) div 2;

and "AutoSize" to "False".
26/11/2013 at 12:02

Positive Negative
88 Votes

But the .Left and .Top properties of the label have no effect to the alignment of the text of the label.

And .Width - .Witdh is always zero leading to exceptions as it is also for .Height - .Height.
26/11/2013 at 15:05

Positive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.