Page 1 of 1

How Find&Replace "anithing in a TrichEdit?

Posted: Thu Nov 16, 2006 9:12 am
by alogrep
Hi
I have a TRichView control with 'regular' text and tables (and maybe other items in the future).
I want to traverse everything in the control and translate it from the original language (as written at design time) to another language.
Example a TRichViewEdit with text and tables.
I want to do: search the whole text and when you find 'Hello' (but not ('Hello2') change it to 'Ciao'. Also I want to say: go and search for all tables and if cell[0,0] text is 'Question' change it to 'Domanda'.
Any easy way?
Thanks.

Posted: Thu Nov 16, 2006 6:08 pm
by Sergey Tkachenko
Should it be an editing operation or not? (editing operations are slower, they can be undone by users).

As for implementing as editing operation, use rve.SearchText

Code: Select all

var ltable: TRVTableItemInfo;
      lrve: TCustomRichViewEdit;

// replacing all occurences of word 'Hello' to 'Ciao'
rve.SetSelectionBounds(0, rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
while rve.SearchText('Hello', [rvseoDown, rvseoWholeWord]) do
  rve.InsertText('Ciao');

// replacing word 'Question' to 'Domanda' in the
// top left cells of tables
rve.SetSelectionBounds(0, rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
while rve.SearchText('Question', [rvseoDown, rvseoWholeWord]) do
  if rve.InplaceEditor<>nil then begin
    lrve := rve;
    while TCustomRichViewEdit(lrve.InplaceEditor).InplaceEditor<>nil do
      lrve := rve;
    ltable := lrve.GetCurrentItem as TRVTableItemInfo;
    if ltable.Cells[0, 0].GetRVData=rve.TopLevelEditor.RVData then
      rve.InsertText('Domanda');
  end;

Posted: Thu Nov 16, 2006 8:30 pm
by alogrep
it would be non editing. What would I substitute in your code (to apply it to non editing)?
Thanks

Posted: Thu Nov 16, 2006 9:07 pm
by Sergey Tkachenko
In the help file, in the topic "Controls, Documents, Items", there is an example, procedure AllUpperCase.
This procedure enumerates all items in the document (include items of table cells) and changes all text to upper case.
You can change this procedure to implement another operation on strings (replacing words instead of changing character case)

Posted: Thu Nov 16, 2006 10:15 pm
by alogrep
I applied that code, but there is a problem:
This is my exact code:

Code: Select all

    with rv1 do begin
       for I := 0 to RVData.Items.Count - 1 do
          if RVData.GetItemStyle(I) >= 0 then begin
            s:= RVData.GetItemTextA(I);
            translatit(s);
            RVData.SetItemTextA(I,s);
          end;
     end;
Now, the original word in line 1 is "Ciao".
Translait translates it to "Hello" (so 's' after the call to translatit is 'Hello', checked with breakpoint) but the richedit control shows "Hell", i.e. it seems thta truncates the word to the length of the original?

Posted: Sun Nov 19, 2006 7:04 pm
by Sergey Tkachenko
call rv1.Format after this code.