59 Votes

Delphi: Multiline Caption for TLabel (at Run Time and Design Time)

Tutorial by Stefan Trost | Last update on 2022-11-18 | Created on 2012-09-05

In this tip, I would like to show you, how you can get a label (TLabel) to display a multi-line caption in Delphi. We would like to look at how we can create multi-line captions at runtime as well as at design time.

First you can adjust the following properties of your label. You can simply do this via the object inspector or you can use the following code (for example in the OnCreate procedure of your form):

with Label1 do begin
   WordWrap  := True;
   AutoSize  := True;
   Align     := alTop;  // or alClient
end;

The property "WordWrap" ensures that the text of your caption is automatically broken to a new line if it is too long for the width of the label. If you want to use a longer text as caption, you should set this property, otherwise the text will not be fully visible.

Furthermore, also the properties of "AutoSize" and "Align" can be useful in connection with multi-line captions so that the size of the label automatically adapts to its content and fully exploits the available space (for example if the label is placed on a panel).

Set multiline Caption during Run Time

First of all, we would like to look at how we can set a multi-line caption for our label at run time of our program. To do this, we just have to incorporate a line break at the point where the line is supposed to break in our string for the caption of the label.

On Windows, the characters #13 and #10 written in a row are standing for a line break (these are the codepoints U+0D and U+0A). Accordingly, in the code, you can create a multi-line caption in the following way:

Label1.Caption := 'Zeile 1' + #13#10 + 'Zeile 2';

Alternatively, you can also use the constant sLineBreak, which is defined in the Unit "System" of Delphi:

Label1.Caption := 'Zeile 1' + sLineBreak + 'Zeile 2';

The advantage of using this variant is that the constant sLineBreak contains the right characters for a line break depending on the system on which we compile our program without having to adjust the code. Our code becomes platform independent with this. Linux and MacOS, for example, only use the character #10 (U+0A) as a line break.

If we create a program using Lazarus instead of Delphi, we can also use the constant LineEnding in the same way like "sLineBreak", which is also declared in the unit "System". Lazarus makes it possible to use the same code to compile a program for example, for Windows as well as for MacOS and Linux. With the use of the constant LineEnding, we do not have to adapt our code for the different like break types of the individual systems.

Create multiline Caption at Design Time

If you already want to set the line break at the design time, you can unfortunately not use the object inspector as usual, since it only allows one-line texts for the caption.

Instead, you can right-click on the label and select the option "View as Text". Somewhere in the new window that opens, you will find the part "object Label1:TLabel", where you can set the Caption with #13#10 like described above.

For example, that could look like this:

object Label1: TLabel
  Left = 0
  Top = 0
  Width = 100
  Height = 13
  Caption = 'Line 1'#13#10'Line 2'
  WordWrap = True
  AutoSize = True
  Align = alTop
end

Then you can again click with the right mouse button on the content of the window and select "View as Form". With this, you get back to the design view and you should already see the line break in the text of the label.

ReplyPositiveNegative
11 Vote

You can also right click on the form or label and select "View source (.lfm)" from the context menu.

Here you can write:

Caption = 'Line1'#13#10'Line2';

This should also do the job.
2015-07-20 at 01:38

ReplyPositive Negative
Reply

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

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 article on askingbox.com. That’s how it’s done.