Forum > RichMemo

RichMemo - Search and Select

(1/1)

Nicole:
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  [+][-]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";}};} ---zeiger:=0; // beginnt am Textanfang zu suchen Fundstelle:=99; // willkürlicher Wert, damit es ungleich -1 für "nicht gefunden" ist Trefferanzahl:=0;  while (Fundstelle <> -1) do begin   // zeiger ist die jeweilige (neue) Suchposition   Fundstelle := RichMemo_Notizen.Search(suche_nach, zeiger, RichMemo_Notizen.GettextLen(), []);    if (Fundstelle <> -1) then begin       RichMemo_Notizen.SelStart:=Fundstelle;       RichMemo_Notizen.SelLength := Length(suche_nach);       Trefferanzahl:=Trefferanzahl + 1;       Memo_FundeRichEdit.Lines.Add('Treffer ' + IntToStr(Trefferanzahl) + ' an Position: ' + IntToStr(Fundstelle));                              end;   if (Fundstelle > zeiger) then       zeiger:=Fundstelle + 1; // sonst findet er dasselbe Wort immer wieder   end;

rvk:

--- Quote from: Nicole on July 20, 2022, 01:34:47 pm ---My problem: Selected is the very last hit. How can I make, that EVERY hit stays selected?
--- End quote ---
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  [+][-]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";}};} ---uses Windows, RichEdit, RichMemoUtils; procedure RichMemoHighlightRange(R: TRichMemo; ASelStart, ASelLength: integer; AColor: TColor);var  Format: CHARFORMAT2;  OldSelStart, OldSelLength: integer;begin  R.Lines.BeginUpdate;  try     OldSelStart := R.SelStart;    OldSelLength := R.SelLength;     R.SelStart := ASelStart;    R.SelLength := ASelLength;     FillChar(Format, SizeOf(Format), 0);    Format.cbSize := SizeOf(Format);    SendMessage(R.Handle, EM_GETCHARFORMAT, WPARAM(True), LPARAM(@Format));    Format.dwMask := CFM_BACKCOLOR;    if AColor = 0 then    begin      Format.dwEffects := CFE_AUTOBACKCOLOR;    end    else    begin      Format.dwEffects := 0;      Format.crBackColor := ColorToRGB(AColor);    end;    SendMessage(R.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));   finally    R.SelStart := OldSelStart;    R.SelLength := OldSelLength;    R.Lines.EndUpdate;  end; end; procedure RichMemoHighlightReset(R: TRichMemo);begin  RichMemoHighlightRange(R, 0, R.GetTextLen, 0);end; procedure RichMemoHighlightWord(R: TRichMemo; AWord: string; AColor: TColor);var  Start, Found: integer;begin  Start := 0;  Found := R.Search(AWord, Start, R.GetTextLen, []);   while (Found <> -1) do  begin    RichMemoHighLightRange(R, Found, Length(AWord), AColor);    Start := Found + 1;    Found := R.Search(AWord, Start, R.GetTextLen, []);  end; end; procedure TForm1.FormCreate(Sender: TObject);begin  LoadRtfFile(RichMemo1, 'c:\temp\test.rtf');end; procedure TForm1.Button1Click(Sender: TObject);begin  RichMemoHighlightReset(RichMemo1);end; procedure TForm1.Button2Click(Sender: TObject);begin  RichMemoHighlightWord(RichMemo1, 'Lorem', clYellow);  RichMemoHighlightWord(RichMemo1, 'Ipsum', clMoneyGreen);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:
thank you!

Navigation

[0] Message Index

Go to full version