Forum > General

search and replace richmemo

<< < (2/3) > >>


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?



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);
  StartPos, EndPos: Integer;
  SearchOptions: TSearchOptions;
  TextParams: TFontParams;
  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
      // 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
          if (fsItalic in TextParams.Style) or (fsBold in TextParams.Style) then
              ARichMemo.SelStart := StartPos;
              ARichMemo.SelLength := EndPos;
              ARichMemo.SelText := ReplaceText;
      StartPos := StartPos + Length(ReplaceText);

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.



--- Quote from: rcmz on May 24, 2024, 07:11:52 pm ---how can I correct this problem.
--- End quote ---
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.

Thx for your reply

i have a text something like this

Dear [name] your account is [account]

so when I replace name with the result is


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.



--- Quote from: rcmz on May 27, 2024, 07:46:17 pm ---I hope this is a litle bit more clear.
--- End quote ---
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.


[0] Message Index

[#] Next page

[*] Previous page

Go to full version