Recent

Author Topic: search and replace richmemo  (Read 4980 times)

rcmz

  • Full Member
  • ***
  • Posts: 137
search and replace richmemo
« on: April 09, 2024, 08:23:36 pm »
hi,

how can i search and replace text that haves font format like italic or bold ?

if I onlye replace the string I lose the font.

TIA
Ramiro

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: search and replace richmemo
« Reply #1 on: April 09, 2024, 09:13:35 pm »
Hello Ramiro, sadly you described it too bad to give you a proper piece of code.
Anyway, here you can look how to check font properties (SLOW!)
Code: Pascal  [Select][+][-]
  1. var
  2.   StartPos, EndPos: Integer;
  3.   TextParams: TFontParams;
  4. begin
  5.   StartPos := 1;
  6.   EndPos := RichMemo1.GetTextLen;
  7.   while StartPos < EndPos do
  8.     begin
  9.       if RichMemo1.GetTextAttributes(StartPos, TextParams) then
  10.         begin
  11.           if (fsItalic in TextParams.Style) or (fsBold in TextParams.Style) then
  12.             begin
  13.               // here you would need to search for the end of font style and/or the actual text to be replaced
  14.               // do not forget to update "StartPos" for the last checked position to skip them in the loop
  15.             end;
  16.         end;
  17.       Inc(StartPos);
  18.     end;
  19. end;
At least this shows some RTF basics :P

//edit
PS: The smarter way would be to first search for the text that you want to replace and on foundings do the above check for style(s) to be sure its that element that needs adjustments.
Here is a snippet to let you know how to actual replace the text without touching the font property:
Code: Pascal  [Select][+][-]
  1. RichMemo1.SelStart := StartPos; // this is the position for the first character
  2. RichMemo1.SelLength := StartPos + Length of the text to be replaced;
  3. RichMemo1.SelText := 'Text that replace original.';
I assume now you are prepared.
« Last Edit: April 09, 2024, 09:35:03 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #2 on: April 09, 2024, 09:35:41 pm »
Hi,

thx, Im trying to build a mailmerge in delphi I used richview and all this hastle is handle by the component, here it is a litle bit more work

this is the code Im using to replace the text

richm1.linex.text := stringreplace(richm1.lines.text, [name], qry.fieldbyname('name').asstring, [rfReplaceAll])

but after I replace it I lose for example Bold.

one other thing, how can I preview to see how it will look once it is printed?

TIA
Ramiro

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: search and replace richmemo
« Reply #3 on: April 09, 2024, 09:45:40 pm »
richm1.linex.text := stringreplace(richm1.lines.text, [name], qry.fieldbyname('name').asstring, [rfReplaceAll])
Watch my edit to learn how to replace text correct.
Your way blow up every text formating/style etc.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: search and replace richmemo
« Reply #4 on: April 09, 2024, 10:06:46 pm »
You can try this method:
Code: Pascal  [Select][+][-]
  1. procedure ReplaceAllBoldItalic(var ARichMemo: TRichMemo; const SearchText, ReplaceText: string);
  2. var
  3.   StartPos, EndPos: Integer;
  4.   SearchOptions: TSearchOptions;
  5.   TextParams: TFontParams;
  6. begin
  7.   SearchOptions := []; // I let options blank, feel free to play with them
  8.   StartPos := 0;
  9.   while ARichMemo.Search(SearchText, StartPos, ARichMemo.GetTextLen, SearchOptions, StartPos, EndPos) do
  10.     begin
  11.       // to be quick, just check first char if it got the wanted style
  12.       // if that is to unprecise for your usage, extend this block to check all positions
  13.       if ARichMemo.GetTextAttributes(StartPos, TextParams) then
  14.         begin
  15.           if (fsItalic in TextParams.Style) or (fsBold in TextParams.Style) then
  16.             begin
  17.               ARichMemo.SelStart := StartPos;
  18.               ARichMemo.SelLength := EndPos;
  19.               ARichMemo.SelText := ReplaceText;
  20.             end;
  21.         end;
  22.       StartPos := StartPos + Length(ReplaceText);
  23.     end;
  24. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #5 on: April 10, 2024, 06:43:55 pm »
hi,

Im trying the procedure you posted, and it replaces the search text and leaves it bold!

but all the paragraph gets repleace with the search text.

what am i missing?

TIA
Ramiro

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #6 on: May 24, 2024, 07:11:52 pm »
Hi,

This is the code Im using, as you can see it's the same as your recomendation.

procedure ReplaceAllBoldItalic(var ARichMemo: TRichMemo; const SearchText, ReplaceText: string);
var
  StartPos, EndPos: Integer;
  SearchOptions: TSearchOptions;
  TextParams: TFontParams;
begin
  SearchOptions := [soMatchCase]; // I let options blank, feel free to play with them
  StartPos := 0;
  while ARichMemo.Search(SearchText, StartPos, ARichMemo.GetTextLen, SearchOptions, StartPos, EndPos) do
    begin
      // to be quick, just check first char if it got the wanted style
      // if that is to unprecise for your usage, extend this block to check all positions
      if ARichMemo.GetTextAttributes(StartPos, TextParams) then
        begin
          if (fsItalic in TextParams.Style) or (fsBold in TextParams.Style) then
            begin
              ARichMemo.SelStart := StartPos;
              ARichMemo.SelLength := EndPos;
              ARichMemo.SelText := ReplaceText;
            end;
        end;
      StartPos := StartPos + Length(ReplaceText);
    end;
end;

this is where i call for replacing the code:

ReplaceAllBoldItalic(rmDoc, 'name', qry.fieldbyname('name').asstring);

but then all the text gets replaced with only qry.fieldbyname('name').asstring.

how can I correct this problem.

TIA
Ramiro

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: search and replace richmemo
« Reply #7 on: May 24, 2024, 09:19:15 pm »
how can I correct this problem.
By describing better what the problem is.
I am unsure how I should interprete your text.
You asked for a method that replaces all "text A" with "text B" without loosing font assignment from "text A" if the condition "is bold or italic" is met and exactly that is what the method does do.
So what you want to have changed or what is not working correct?
The error might be not in my snippet.

Upload a minimalistic source project that show how you add text to richmemo in different ways, also bold and italic mixed inside one or many lines and how you then call my snippet plus show as image or text what you had expected and what it currently does do wrong.
For testing switch in your demo to TEdit instead of "qry.fieldbyname('name').asstring" so we (I) does not need to create a database table just to have a string for testing.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #8 on: May 27, 2024, 07:46:17 pm »
Thx for your reply

i have a text something like this

Dear [name] your account is [account]

so when I replace name with qry.field.name the result is

Kevin

and I lose all the other code, and the only replace text is name and account does not get even replaced.

I hope this is a litle bit more clear.

TIA
ramiro

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: search and replace richmemo
« Reply #9 on: May 27, 2024, 08:12:12 pm »
I hope this is a litle bit more clear.
No. I do not see in your text how you fill the RichMemo like i asked you.
So again, create a new project with those 3 things:
1. A TRichMemo
2. A TEdit
3. A TButton

required things in that test project:
Fill the TRichMemo with some test text and be sure that some words have fsBold or fsItalic set.
Fill the TEdit with a word or phrase that is in TRichMemo present.
Let TButton call my method.

When that is done, zip your test project and upload it here so we can go on.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #10 on: May 28, 2024, 08:23:12 pm »
hi

the difference against my program is that the text is saved in a table and
gets loaded, text replace and send to print

I could not replicate this part, but in the demo you can select text and assign bold

in the print button the code will be replaced

in the demo all works according to your suggested code.

the difference between the demo and my code is:

the text as i mentioned is fill via table, after that, the text thats mark for replace gets replaced, the stream is closed, and print is executed.

the text is not visible in this part of the program.

TIA
Ramiro

ps.
hope you can find what am i doing wrong.

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: search and replace richmemo
« Reply #11 on: May 29, 2024, 09:49:00 pm »
hi,

sending a new zip with modifications so that the file is read from RTF and with the bold attrib.

in the code I explain some problems at this moment not resolve.

TIA
Ramiro

ps
I install paperless printer. it stil does not print.

 

TinyPortal © 2005-2018