Page 1 of 1

How to iterate all words in TRichView ?

Posted: Tue Jun 24, 2008 2:08 am
by nostradumbass
Hi.

I am using TRichView component. I want to iterate all the words in the content, but am not able to do this, as GetItemText returns the paragraph. I need each individual word, because I am doing a find/replace operation.

Please help me how to do it. Thanks!

Regards,
NotraDamus

Here is my code:

Code: Select all

var
 richviewedit1 : TRichView;
 richviewstyle1 : TRVStyle;
 c,d :integer;
 s:string;
begin

 richviewedit1 := TRichView.Create(nil);
 richviewstyle1 := TRVStyle.Create(nil);
 richviewedit1.Parent:=application.MainForm;
 richviewedit1.Style:=richviewstyle1;

 emlmsg.MsgBody.Position:=0;
 richviewedit1.LoadRTFFromStream( emlmsg.MsgBody );
 richviewedit1.Format;

 for c := 0 to richviewedit1.itemCount - 1 do
 begin
   if richviewedit1.GetItemStyle(c) >= 0 then
   begin
     for d := 0 to replaceables.FindList.Count - 1 do
     begin
       if  trim(richviewedit1.GetItemText(c)) = trim(replaceables.FindList[d]) then
       begin
         richviewedit1.SetItemText(c, replaceables.replaceList[d]);
         richviewedit1.Format;
       end;
     end;
   end;
 end;

 emlmsg.MsgBody.Position:=0;
 richviewedit1.SaveRTFToStream( emlmsg.MsgBody, false );
 
 richviewedit1.Parent:=nil;
 richviewedit1.Free;
end;

Posted: Tue Jun 24, 2008 7:50 am
by Sergey Tkachenko
This is the fastest way to replace text in TRichView.
But if document contains tables, your code does not replace text in table cells. To process tables, create your procedure basing on AllUpperCase example.
And remove all these calls of Format, call Format only one time, before SaveRTFToStream.

There is a word enumerator class, used by spell checkers, but it will be much slower, because it was designed to replace words as an editing operation (undoable).
Afer you get the item text (it's better to use GetItemTextA/SetItemTextA instead of GetItemText/SetItemText), you can separate it into words yourself.