How to find/replace text on DBRichViewEdit

General TRichView support forum. Please post your questions here
Post Reply
Kym
Posts: 18
Joined: Fri Mar 24, 2006 1:57 pm

How to find/replace text on DBRichViewEdit

Post by Kym »

Hello, I need to find text and replace these text with another using DBRichViewEdit.

For example, I have the following text:

R.<register_number>-M.<math_numer>.

I'll get all "params" that are between '<' and '>', and ask the user:

What's the register number?

Then I go to the text and replace <register_number> with the number that the user typed, like 1, 10, 100, 1000.

I'm using DBRichViewEdit and I only have text or formatted text (like bold, underline) stored on the DataBase, saved as RTF.

How can I do that? Thanks.
Kym
Posts: 18
Joined: Fri Mar 24, 2006 1:57 pm

Post by Kym »

By the way, I can't use Dialogs to replace the params, because I'll have a form where the user set all params, than I need to replace them all once at a time.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

See here: http://www.trichview.com/forums/viewtopic.php?t=8

See mailmerge-text.zip demo
Kym
Posts: 18
Joined: Fri Mar 24, 2006 1:57 pm

Post by Kym »

Sergey Tkachenko wrote:See here: http://www.trichview.com/forums/viewtopic.php?t=8

See mailmerge-text.zip demo
I saw that, but I cant figure out how to configure it :(

Because I'll have 3 types of params in the text, some between '<' and '>', other between '{' and '}' and other between '[' and ']'.

Each type of param requires a particular form to be filled, so I need to get them, show the form, user fill it, then I go to the next type, show the form and so on..

:/
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

The main procedure in this demo is FillFields (MMMainForm.pas)
It enumerates all items in the document. When a text item is found (if RVData.GetItemStyle(i)>=0 then), it gets its text (s := RVData.GetItemTextA(i)). Then it enumerates all characters in s, from the last character to the first character. If '}' is found, it searches for the matching '{'. If found, it assigns the text between {} to FieldName variable, calculates the field value (FieldValue := GetFieldValueFromDatabase(FieldName)), and replace the field name with the field value in s.
After that, s is assigned back to the text item.
All this processing is in this code:

Code: Select all

      s := RVData.GetItemTextA(i);
      FieldEnd := 0;
      Changed := False;
      for j := Length(s) downto 1 do begin
        if s[j]='}' then
          FieldEnd := j
        else if (s[j]='{') and (FieldEnd>0) then begin
          FieldName :=  Copy(s, j+1, FieldEnd-j-1);
          FieldValue := GetFieldValueFromDatabase(FieldName);
          Delete(s, j, FieldEnd-j+1);
          Insert(FieldValue, s, j);
          FieldEnd := 0;
          Changed := True;
        end;
      end;
      if Changed then
        RVData.SetItemTextA(i, s);
You need to modify:
1) Add code searching for '[' if ']' is found, and for '<' if '>' is found (you should decide what to do if they are nested)
2) Add a new parameter to GetFieldValueFromDatabase, identifying a type of brackets.

As you can see, all this code works with text string, not with TRichView
Post Reply