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!
Insert formatted or unformatted text to same method?
-
- Site Admin
- Posts: 17569
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
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;
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.
---------------------------
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.
-
- Site Admin
- Posts: 17569
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
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:
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.
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;