1 3 Votes

Delphi: Multiline Caption for TLabel (at design time)

Tip by Delphian | 05/09/2012 at 12:52

In this tip, I want to show you, how you get a label to display a multi-line caption.

First of all, you need to adjust some properties of the label. You can do that by using the Object Inspector or you can use the following code:

with Label1 do begin
   WordWrap := True;
   Alignment := taCenter;
   Layout := tlCenter;
   AutoSize := False;

Most important is the property "WordWrap", which ensures that the text will automatically wrap to the next line if it is too long. If you want to have the text written centered on the label, you must also set the properties of "Alignment" and "Layout" to middle. Finally, setting "AutoSize" to "False", prevents the label to go back to its minimum size after starting the program.

Set multiline caption at runtime

In Delphi code, #13#10 stands for a line break. Therefore, you can set a caption over multiple lines in the code with the following command:

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

Accordingly, this happens during the program is running (run time). If you would like to set the line break before, at design time, unfortunately, you cannot use the Object Inspector for this.

Set multiline caption at design time

Instead, you have to right-click on the label and select the option "View as Text". Somewhere in the new window, you will find the part "object Label1:TLabel", where you can set the Caption with #13#10 like mentioned above. Then, you have to right-click again on the content of the window and select "View as Form" to get back to the design view of your form.

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.
20/07/2015 at 01:38

ReplyPositive Negative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message


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.


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