tags:

views:

8996

answers:

8

I want to create a string that spans multiple lines to assign to a Label Caption property. How is this done in Delphi?

+5  A: 

my_string := 'Hello,' + #13#10 + 'world!';

#13#10 is the CR/LF characters in decimal

Brendan
And it still works for 2009 (Unicode).
Gamecat
+10  A: 

here's an even shorter approach:

my_string := 'Hello,'#13#10' world!';

Zartog
+4  A: 

On the side, a trick that can be useful:
If you hold your multiple strings in a TStrings, you just have to use the Text property of the TStrings like in the following example.

Label1.Caption := Memo1.Lines.Text;

And you'll get your multi-line label...

François
+3  A: 

Or you can use the ^M+^J shortcut also. All a matter of preference. the "CTRL-CHAR" codes are translated by the compiler.

MyString := 'Hello,' + ^M + ^J + 'world!';

You can take the + away between the ^M and ^J, but then you will get a warning by the compiler (but it will still compile fine).

skamradt
A: 

I prefer to define "const LFCR = #13#10;" because I end up using that all over the place in my code. Then it's simple to writing it like this:

MyString := 'Hello,' + LFCR + 'world!';

Mason Wheeler
Ahm, CR = #13 and LF = #10 so that should be const CRLF.
gabr
*shrugs* Something like that. :P
Mason Wheeler
sLineBreak is a predefined constant.
Jim McKeeth
Yes it is. But its value changes across different platforms, so it's not suitable if you're writing cross-platform code that involves saving and loading strings. (If you're not, feel free to use it.)
Mason Wheeler
If you are writing cross-platform code that involves saving and loading strings, then you need to use some special character when saving and loading, and then replace it with the correct line break (from sLineBreak perhaps) for the current platform, otherwise it will display incorrectly. If you just use the const you specified then it will display incorrectly on Linux and Mac.
Jim McKeeth
depends what you mean by cross-platform. If you are writing Linux code that is required to read/write PC files then you don't want to use "constants" that vary from platform to platform, you want to be certain that you are using the appropriate PC-platform constant. Inter-operating code might be a better description for that scenario but in this context I think this may be what MW had in mind by "cross-platform" in this case.
Deltics
+27  A: 

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.

Jim McKeeth
Thanks for that! I've always used #13#10.
Osama ALASSIRY
Wow, that's a lot better than I had hoped for!
Brendan
A: 

Hi

I dont have a copy of Delphi to hand, but I'm fairly certain if you set the wordwrap property to true and the autosize property to false it should wrap any text you put it at the size you make the label. If you want to line break in a certain place then it might work if you set the above settings and paste froma text editor.

Hope this helps.

Toby Allen
A: 

ShowMessage('Hello'+Chr(10)+'World');