Insert formatted or unformatted text to same method?

General TRichView support forum. Please post your questions here
Post Reply
Lina
Posts: 9
Joined: Wed Jan 10, 2007 9:06 am

Insert formatted or unformatted text to same method?

Post by Lina »

Hi!

I need to insert two different kinds of text into TRichView. Sometimes it is RTF-formatted and sometimes it is not formatted at all. When unformatted text is inserted I get an exception when TRichView tries to figure out what font the text is in. Is there a method or something I can use to insert text that might be formatted or unformatted?


Thanks!
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

How do you insert these texts?
Lina
Posts: 9
Joined: Wed Jan 10, 2007 9:06 am

Post by Lina »

Like this:

Code: Select all

procedure TCustomRichViewEdit.SetText(sText: string);
var
 rtfStream : TStringStream;
begin
  rtfStream := TStringStream.Create(sText);
  Self.Clear;
  Self.InsertRTFFromStreamEd(rtfStream);
  rtfStream.Free;
end;
Lina
Posts: 9
Joined: Wed Jan 10, 2007 9:06 am

Post by Lina »

It is only when i use the RVEdit inbedded in another form i get the exception:

---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EListError with message 'List index out of bounds (0)'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------

It is this row that generates the exception:

FN := AnsiLowerCase(Reader.FontTable[FontIndex].Name)

Rownr 2641 in RVRTFProps.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

RTF reading procedure must be able to read plain text files. May be you use old version of TRichView? In any case, it's not a very good idea, because text files may include {,} and \ characters, and they will be treated as RTF keywords.

You can check the beginning of sText.
If it is started from '{\rtf', then call InsertRTFFromStreamEd, otherwise call InsertText.
(InsertRTFFromStreamEd and InsertText are editing-style methods, they require formatted document before their call, so call Self.Format after Self.Clear and BEFORE these methods)

Editing-style operations can be undone by user. Is it really necessary in your case? I guess not. In this case, you can use LoadFromStream:

Code: Select all

procedure TCustomRichViewEdit.SetText(sText: string); 
var 
 rtfStream : TStringStream; 
begin 
  rtfStream := TStringStream.Create(sText); 
  Self.Clear; 
  Self.LoadFromStream(rtfStream, rvynaNo); 
  Self.Format;
  rtfStream.Free; 
end;
LoadFromStream autodetects format of stream. If you change TStringStream to TMemoryStream, your method will work not only with RTF and plain text, but also with RVF.
Lina
Posts: 9
Joined: Wed Jan 10, 2007 9:06 am

Post by Lina »

Thank you for your answer, my editor works great after the code change you recommended :)
Post Reply