search and replace richmemo lazarus / 21-General ---------------------------------------------------------------------- rcmz · 2024-04-09 18:23 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 · 2024-04-09 19:13 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  StartPos, EndPos: Integer;  TextParams: TFontParams;begin  StartPos := 1;  EndPos := RichMemo1.GetTextLen;  while StartPos < EndPos do    begin      if RichMemo1.GetTextAttributes(StartPos, TextParams) then      begin          if (fsItalic in TextParams.Style) or (fsBold in TextParams.Style) then            begin              // here you would need to search for the end of font style and/or the actual text to be replaced              // do not forget to update "StartPos" for the last checked position to skip them in the loop            end;        end;      Inc(StartPos);    end;end;At least this shows some RTF basics //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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RichMemo1.SelStart := StartPos; // this is the position for the first characterRichMemo1.SelLength := StartPos + Length of the text to be replaced;RichMemo1.SelText := 'Text that replace original.';I assume now you are prepared. ---------------------------------------------------------------------- rcmz · 2024-04-09 19:35 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 · 2024-04-09 19:45 Quote from: rcmz on April 09, 2024, 09:35:41 pmrichm1.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. ---------------------------------------------------------------------- KodeZwerg · 2024-04-09 20:06 You can try this method: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ReplaceAllBoldItalic(var ARichMemo: TRichMemo; const SearchText, ReplaceText: string);var  StartPos, EndPos: Integer;  SearchOptions: TSearchOptions;  TextParams: TFontParams;begin  SearchOptions := []; // 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; ---------------------------------------------------------------------- rcmz · 2024-04-10 16:43 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 · 2024-05-24 17:11 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 · 2024-05-24 19:19 Quote from: rcmz on May 24, 2024, 07:11:52 pmhow 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. ---------------------------------------------------------------------- rcmz · 2024-05-27 17:46 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 · 2024-05-27 18:12 Quote from: rcmz on May 27, 2024, 07:46:17 pmI 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. ---------------------------------------------------------------------- rcmz · 2024-05-28 18:23 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 · 2024-05-29 19:49 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.