Recent

Author Topic: RichMemo - Search and Select  (Read 329 times)

Nicole

  • Sr. Member
  • ****
  • Posts: 496
RichMemo - Search and Select
« on: July 20, 2022, 01:34:47 pm »
The code below does this:

- searches my TRichMemo
- lists the hits
- selcts the hit(s) within the TRichMemo => problem

My problem: Selected is the very last hit. How can I make, that EVERY hit stays selected?

 
Code: Pascal  [Select][+][-]
  1. zeiger:=0; // beginnt am Textanfang zu suchen
  2.  Fundstelle:=99; // willkürlicher Wert, damit es ungleich -1 für "nicht gefunden" ist
  3.  Trefferanzahl:=0;
  4.  
  5.  while (Fundstelle <> -1) do begin   // zeiger ist die jeweilige (neue) Suchposition
  6.    Fundstelle := RichMemo_Notizen.Search(suche_nach, zeiger, RichMemo_Notizen.GettextLen(), []);
  7.  
  8.    if (Fundstelle <> -1) then begin
  9.        RichMemo_Notizen.SelStart:=Fundstelle;
  10.        RichMemo_Notizen.SelLength := Length(suche_nach);
  11.        Trefferanzahl:=Trefferanzahl + 1;
  12.        Memo_FundeRichEdit.Lines.Add('Treffer ' + IntToStr(Trefferanzahl) + ' an Position: ' + IntToStr(Fundstelle));
  13.                               end;
  14.    if (Fundstelle > zeiger) then
  15.        zeiger:=Fundstelle + 1; // sonst findet er dasselbe Wort immer wieder
  16.    end;

rvk

  • Hero Member
  • *****
  • Posts: 5017
Re: RichMemo - Search and Select
« Reply #1 on: July 21, 2022, 11:24:10 am »
My problem: Selected is the very last hit. How can I make, that EVERY hit stays selected?
You can't. At least not with TRichMemo.SelStart/SelLength. Those are just for one selection.

The only thing you can do is apply highlighting to your underlying RTF so words you want selected are highlighted.

Just a quick and dirty piece of code I thrown together...
(test.rtf contains some random text from https://www.lipsum.com/)

Code: Pascal  [Select][+][-]
  1. uses Windows, RichEdit, RichMemoUtils;
  2.  
  3. procedure RichMemoHighlightRange(R: TRichMemo; ASelStart, ASelLength: integer; AColor: TColor);
  4. var
  5.   Format: CHARFORMAT2;
  6.   OldSelStart, OldSelLength: integer;
  7. begin
  8.   R.Lines.BeginUpdate;
  9.   try
  10.  
  11.     OldSelStart := R.SelStart;
  12.     OldSelLength := R.SelLength;
  13.  
  14.     R.SelStart := ASelStart;
  15.     R.SelLength := ASelLength;
  16.  
  17.     FillChar(Format, SizeOf(Format), 0);
  18.     Format.cbSize := SizeOf(Format);
  19.     SendMessage(R.Handle, EM_GETCHARFORMAT, WPARAM(True), LPARAM(@Format));
  20.     Format.dwMask := CFM_BACKCOLOR;
  21.     if AColor = 0 then
  22.     begin
  23.       Format.dwEffects := CFE_AUTOBACKCOLOR;
  24.     end
  25.     else
  26.     begin
  27.       Format.dwEffects := 0;
  28.       Format.crBackColor := ColorToRGB(AColor);
  29.     end;
  30.     SendMessage(R.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));
  31.  
  32.   finally
  33.     R.SelStart := OldSelStart;
  34.     R.SelLength := OldSelLength;
  35.     R.Lines.EndUpdate;
  36.   end;
  37.  
  38. end;
  39.  
  40. procedure RichMemoHighlightReset(R: TRichMemo);
  41. begin
  42.   RichMemoHighlightRange(R, 0, R.GetTextLen, 0);
  43. end;
  44.  
  45. procedure RichMemoHighlightWord(R: TRichMemo; AWord: string; AColor: TColor);
  46. var
  47.   Start, Found: integer;
  48. begin
  49.   Start := 0;
  50.   Found := R.Search(AWord, Start, R.GetTextLen, []);
  51.  
  52.   while (Found <> -1) do
  53.   begin
  54.     RichMemoHighLightRange(R, Found, Length(AWord), AColor);
  55.     Start := Found + 1;
  56.     Found := R.Search(AWord, Start, R.GetTextLen, []);
  57.   end;
  58.  
  59. end;
  60.  
  61. procedure TForm1.FormCreate(Sender: TObject);
  62. begin
  63.   LoadRtfFile(RichMemo1, 'c:\temp\test.rtf');
  64. end;
  65.  
  66. procedure TForm1.Button1Click(Sender: TObject);
  67. begin
  68.   RichMemoHighlightReset(RichMemo1);
  69. end;
  70.  
  71. procedure TForm1.Button2Click(Sender: TObject);
  72. begin
  73.   RichMemoHighlightWord(RichMemo1, 'Lorem', clYellow);
  74.   RichMemoHighlightWord(RichMemo1, 'Ipsum', clMoneyGreen);
  75. end;

Did you mean something like this (attached image)?
The reset button resets the highlighting.

(Of course if you have real highlighting in your source rtf, you can't use this method.)


Nicole

  • Sr. Member
  • ****
  • Posts: 496
Re: RichMemo - Search and Select
« Reply #2 on: July 21, 2022, 12:52:49 pm »
thank you!

 

TinyPortal © 2005-2018