Newline Inside a String To Be Shown At a TMemoBox
Asked Answered
L

5

9

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?

Lum answered 22/12, 2010 at 15:31 Comment(2)
duplicated ... #254907Aviary
If it's duplicated, so why the answers are totally different?Lum
F
27

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;
Forehead answered 22/12, 2010 at 15:35 Comment(3)
Can also use sLineBreak.Seacock
using sLineBreak is the best approach!!Donyadoodad
@Sertac Akyuz - +1 great tip, I haven't noticed something like thisForehead
S
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('');
Siddon answered 22/12, 2010 at 15:39 Comment(0)
J
3

Use

FullMemo := txtFistMemo.Text + #13#10 + txtDetails.Text
Jauch answered 22/12, 2010 at 15:36 Comment(0)
A
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

Anecdotage answered 2/1, 2011 at 18:26 Comment(0)
B
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.

Brewhouse answered 22/12, 2010 at 15:35 Comment(3)
Line break on Windows is always #13#10 (or better sLineBreak as Sertac suggests).Trinitytrinket
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.Brewhouse
@Brewhouse - "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.Anecdotage

© 2022 - 2024 — McMap. All rights reserved.