delphinewlinetlabel

How do I include a newline character in a string in Delphi?


I want to create a string that spans multiple lines to assign to a Label's Caption property.

How is this done in Delphi?


Solution

  • 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.