Page 1 of 1
How to find/replace text on DBRichViewEdit
Posted: Fri May 12, 2006 8:34 pm
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.
Posted: Fri May 12, 2006 8:43 pm
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.
Posted: Mon May 15, 2006 2:18 pm
by Sergey Tkachenko
Posted: Tue May 16, 2006 1:57 am
by Kym
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..
:/
Posted: Tue May 16, 2006 10:16 am
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