Forum > General

search and replace richmemo

(1/3) > >>

rcmz:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 :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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:

--- Quote from: rcmz on April 09, 2024, 09:35:41 pm ---richm1.linex.text := stringreplace(richm1.lines.text, [name], qry.fieldbyname('name').asstring, [rfReplaceAll])
--- End quote ---
Watch my edit to learn how to replace text correct.
Your way blow up every text formating/style etc.

KodeZwerg:
You can try this method:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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;

Navigation

[0] Message Index

[#] Next page

Go to full version