1. You do not seem to filter out the 'correct' record field (row) which, in case you show multiple rows at once (columns as well) means that every time the drawgridColumnCell
is triggered (which is for every visible and selected (see gdSelected in state) row/column) your search method(s) are executed.
For now, I leave that for/to yourself to address (and in case you want to).
2. the outer-loop (for i := 0 to UTF8Length(RichMemo1.Text) is unnecessary (as already mentioned before). Simply remove that, see also snippet posted below.
3. You are providing the length of your search term to the call to the richmemo search method (which is wrong, it should be the length of the content to search in , e.g. the length of the Memo contents (the earlier posted code-snipped showed that already). See earlier posted snippet and updated snippet below (which now also takes the length of the search term into consideration and skips it).
4. The loops with the Search and SetTextAttributes methods should be executed somewhere else. I tried to set the text using the text property but unfortunately that does not seem to trigger an onchange event. Also Update and invalidate where not able to trigger an OnChange Event so I have no clue which event to use to perform your search and highlighting routines.
The snippet below seem to work for me (except the aforementioned issues). I leave it up to you to address the length/UTF8len/UTF8Length mayhem's.
procedure TForm1.PerformHighLight;
var
fp : TFontParams;
posSW1 : integer = 0;
SearchTerm1 : string = 'text to search for';
begin
fp.Color := clRed;
fp.Style := [fsBold];
fp.Size := 13;
fp.BkColor := clWhite;
posSW1 := 0;
while (posSW1 < RichMemo1.GetTextLen) and (posSW1 <> -1) do
begin
PosSW1 := RichMemo1.Search(SearchTerm1, PosSW1, RichMemo1.GetTextLen - PosSW1, []);
if posSW1 > 0 then
begin
// set attribute
RichMemo1.SetTextAttributes(PosSW1, Length(SearchTerm1), fp);
// next search-position
inc(PosSW1, Length(SearchTerm1));
end;
end;
// repeat for SW2 and SW3
end;