1

I am having a problem copying the contents of a memo to a richedit component.

I thought it would be

Richedit.text := memo.text;

However if I use this the Richedit starts a new line when the memo text wraps to a new a new line (not CR/LF) but just wrapping. The richedit also starts a new line when the memo starts a new line which is fine.

Anyone got any idea's how to copy the text from a memo into the richeditbox without the lines breaking in the Richedit when the memo text wraps

Thanks

Colin

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
colin
  • 2,983
  • 6
  • 41
  • 49

2 Answers2

2

When I do

RichEdit1.Text := Memo1.Text

the virtual "line-breaks" of the Memo1 are not magically converted to line-breaks (CRLF) in the RichEdit, and they shouldn't be. These "line-breaks" are not stored in the memo text buffer. Indeed, the official Embarcadero documentation states

Set WordWrap to true to make the edit control wrap text at the right margin so it fits in the client area. The wrapping is cosmetic only. The text does not include any return characters that were not explicitly entered.

Anyhow, an alternative way is to do

RichEdit1.Lines.Assign(Memo1.Lines);

although this will preserve the virtual line-breaks, as commented below.

Update

Most likely you have some other strangeness (bug) in your code, or you have phrased your question in a too vague manner. However, to eliminate the risk of any problem with the VCL wrappers, try this:

procedure TForm4.FormClick(Sender: TObject);
var
  buf: PChar;
const
  MAX_BUF_SIZE = 65536;
begin
  GetMem(buf, MAX_BUF_SIZE * sizeof(char));
  Memo1.Perform(WM_GETTEXT, MAX_BUF_SIZE, buf);
  RichEdit1.Perform(WM_SETTEXT, 0, buf);
  FreeMem(buf);
end;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • these are not linebreaks at all, in the fact – Free Consulting Nov 24 '10 at 23:27
  • No, they are sometimes called "virtual line-breaks", as I wrote. – Andreas Rejbrand Nov 24 '10 at 23:30
  • 2
    Actually when I try RichEdit1.Lines.Assign(Memo1.Lines) the virtual line-breaks do transfer in to the RichEdit. That actually makes sense. The underlying TStringList has one item for each drawn line, not just the hard breaks. I'm more confused on how the assigning of the Text property seems to get the breaks correct. – Mark Elder Nov 24 '10 at 23:37
  • 2
    @Mark, when you read the `Text` property, the memo sends itself a `wm_GetText` message to read the entire string as the underlying edit control has it. It doesn't use the base `TStrings` class's method of concatenating each item, separated by line breaks. – Rob Kennedy Nov 25 '10 at 00:20
  • 2Andreas Rejbrand: care to include prooflink on those "virtual line-breaks"? Currently, i'm convinced what text being *drawn* unchanged at all. TMemo and alike controls don`t have 64k limit for now, btw – Free Consulting Nov 25 '10 at 00:38
  • I have tried the above I receive an error at the line Memo.Perform(WM_GETTEXT, MAX_BUF_SIZE, buf); incompatable types:integer and PAnsiChar Colin – colin Nov 25 '10 at 09:52
  • @user435406, you should typecast buf to Integer; Integer(buf). That's because WParam and LParam parameters are just integer parameters. – vcldeveloper Nov 25 '10 at 11:38
0

As a dirty hack, could you switch off word wrap on your memo then do the assignment and then switch word wrap back on? It's a nasty hack but it might do the trick for you if there is some odd behaviour.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124