Loading Unicode string in Delphi XE

General TRichView support forum. Please post your questions here
Post Reply
marekjed
Posts: 4
Joined: Tue Nov 01, 2005 8:19 pm

Loading Unicode string in Delphi XE

Post by marekjed »

I apologize for the beginner's question. I've read "How to make plain text editor" and "How to make Unicode editor" in the Examples forum.

Using RichViewEdit in plain text mode, I'm using LoadTextFromStreamW to load a string (UnicodeString) into the editor. The result is garbled text (e.g. Chinese characters for a simple a-z string), and sometimes (depending on contents of the string) an exception: "Can't perform Unicode operation".

If I use LoadTextFromStream (no -W), the result is fine for text that "fits" in current codepage, but in other cases (Greek, Russian, Chinese) the text is loaded as question marks (typical result of processing Unicode text with ANSI methods).

All the settings described in "How to make Unicode editor" are correct (text styles set to Unicode, etc.)

The code I use is as follows:

Code: Select all

procedure TEditForm.SetAsText(const Value: string);
var
  ss : TStringStream;
begin
  ss := TStringStream.Create( Value );
  try
    ss.Position := 0;
    RVEditor.Clear;
    RVEditor.LoadTextFromStreamW( ss, 0, 0, True );
    RVEditor.Format;
  finally
    ss.Free;
  end;
end;
marekjed
Posts: 4
Joined: Tue Nov 01, 2005 8:19 pm

Post by marekjed »

Solved: TStringStream must be created with TEncoding.Unicode as second parameter to the constructot, otherwise an ANSI stream gets created.
Post Reply