8

I'm building a String called FullMemo, that would be displayed at a TMemoBox, but the problem is that I'm trying to make newlines like this:

FullMemo := txtFistMemo.Text + '\n' + txtDetails.Text

What I got is the content of txtFirstMemo the character \n, not a newline, and the content of txtDetails. What I should do to make the newline work?

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300

5 Answers5

26

The solution is to use #13#10 or better as Sertac suggested sLineBreak.

FullMemo := txtFistMemo.Text + #13#10 + txtDetails.Text;
FullMemo := txtFistMemo.Text + sLineBreak + txtDetails.Text;
4

A more platform independent solution would be TStringList.

var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    Strings.Assign(txtFirstMemo.Lines); // Assuming you use a TMemo
    Strings.AddStrings(txtDetails.Lines);
    FullMemo := Strings.Text;
  finally
    Strings.Free;
  end;
end;

To Add an empty newline you can use:

Strings.Add('');
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
3

Use

FullMemo := txtFistMemo.Text + #13#10 + txtDetails.Text
Bharat
  • 6,828
  • 5
  • 35
  • 56
1

You can declare something like this:

const 
 CRLF = #13#10; //or name it 'Enter' if you want
 LBRK = CRLF+ CRLF;

in a common unit and use it in all your programs. It will be really handy. Now, after 20 years, I have CRLF used in thousands of places!

FullMemo := txtFistMemo.Text + CRLF + txtDetails.Text

IMPORTANT
In Windows, the correct format for enters is CRLF not just CR or just LF as others sugges there. For example Delphi IDe (which is a Windows app) will be really mad at you if your files do not have proper enters (CRLF): Delphi XE - All blue dots are shifted with one line up

Gabriel
  • 20,797
  • 27
  • 159
  • 293
0

You don't make newlines like this, you use symbol #13:

FullMemo := txtFistMemo.Text + #13 + txtDetails.Text
    + Chr(13) + 'some more text'#13.

#13 is CR, #10 is LF, sometimes it's enough to use just CR, sometimes (when writing text files for instance) use #13#10.

himself
  • 4,806
  • 2
  • 27
  • 43
  • 1
    Line break on Windows is always #13#10 (or better sLineBreak as Sertac suggests). – Jens Mühlenhoff Dec 22 '10 at 17:29
  • It's not "always" #13#10. There's no rule saying you cannot parse #13 for linebreak, even on Windows. For instance, MessageBox accepts it just fine. – himself Dec 22 '10 at 18:06
  • @himself - "it just works" is not equivalent to "it is correct". If Delphi editor chateches you with such "half-enters" it will be really mad at you during debugging. – Gabriel Feb 19 '21 at 09:26