I want to create a string that spans multiple lines to assign to a Label Caption property. How is this done in Delphi?
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
So if you want to make your TLabel wrap, make sure AutoSize is set to true, and then use the following code: label1. Caption := 'Line one'+sLineBreak+'Line two'; Works in all versions of Delphi since sLineBreak was introduced, which I believe was Delphi 6.
in a string prevents actually making a new line and instead of Type (new line) for a new line it is Type \n for a new line .
LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.
In the System.pas (which automatically gets used) the following is defined:
const sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF} {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};
This is from Delphi 2009 (notice the use of AnsiChar and AnsiString). (Line wrap added by me.)
So if you want to make your TLabel wrap, make sure AutoSize is set to true, and then use the following code:
label1.Caption := 'Line one'+sLineBreak+'Line two';
Works in all versions of Delphi since sLineBreak was introduced, which I believe was Delphi 6.
Here's an even shorter approach:
my_string := 'Hello,'#13#10' world!';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With