Page 1 of 1

Insert formatted or unformatted text to same method?

Posted: Mon Mar 19, 2007 8:07 am
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!

Posted: Mon Mar 19, 2007 8:48 am
by Sergey Tkachenko
How do you insert these texts?

Posted: Mon Mar 19, 2007 9:00 am
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;

Posted: Mon Mar 19, 2007 5:07 pm
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.

Posted: Mon Mar 19, 2007 7:14 pm
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.

Posted: Tue Mar 20, 2007 1:14 pm
by Lina
Thank you for your answer, my editor works great after the code change you recommended :)