开发者

Newline Inside a String To Be Shown At a TMemoBox

开发者 https://www.devze.com 2023-01-31 15:37 出处:网络
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:

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?


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;


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


Use

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消