Newline Inside a String To Be Shown At a TMemoBox

25,326

Solution 1

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('');

Solution 2

Use

FullMemo := txtFistMemo.Text + #13#10 + txtDetails.Text

Solution 3

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

Share:
25,326
Nathan Campos
Author by

Nathan Campos

Electrical Engineer, Ham radio operator, photographer, used to be a programmer.

Updated on February 19, 2021

Comments

  • Nathan Campos
    Nathan Campos about 3 years

    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?

  • Jens Mühlenhoff
    Jens Mühlenhoff over 13 years
    Line break on Windows is always #13#10 (or better sLineBreak as Sertac suggests).
  • himself
    himself over 13 years
    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.
  • Server Overflow
    Server Overflow about 3 years
    @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.