Einzelnen Beitrag anzeigen

Willie1

Registriert seit: 28. Mai 2008
618 Beiträge
 
Delphi 10.1 Berlin Starter
 
#3

AW: TFindDialog und F3-Taste für Weitersuche

  Alt 10. Jun 2019, 17:20
Hallo,
ich sehe, dass es mit TRichEdit besser ginge aber ich kann und will mein Projekt nicht mehr ändern. Ich muss mich mit TMemo herumschlagen. Meine Suche mit dem TFindDialog ist fertig nur die Integration von Suche nach "ganzen Worten" will mir nicht gelingen. Außerdem würde ich gerne <F3> zur Weitersuche nutzen wollen.
Delphi-Quellcode:
procedure TForm1.FindDialog1Find(Sender: TObject);
const
  TextDelimeter = [#13,#10,' ',',','.','-',';','#','|','<','>','!','?','*','+'];
var
  memoStr, searchStr : string;
  startpos : integer;
  MatchCase,
  WholeWord : Boolean;
begin
  with TFindDialog(Sender) do
  begin
    MatchCase := not (frMatchCase in Options);
    WholeWord := frWholeWord in Options;
    {If the stored position is 0 this cannot be a find next. }
    if FSelPos = 0 then
      Options := Options - [frFindNext];

     { Figure out where to start the search and get the corresponding
       text from the memo. }

    if frfindNext in Options then
    begin
      { This is a find next, start after the end of the last found word. }
      StartPos := FSelPos + Length(Findtext);
      memoStr := Copy(Memo1.Lines.Text, StartPos, MaxInt);
    end
    else
    begin
      { This is a find first, start at the, well, start. }
      memoStr := Memo1.Lines.Text;
      StartPos := 1;
    end;
    if MatchCase then memoStr := AnsiLowerCase(memoStr);
    if MatchCase then searchStr := AnsiLowerCase(FindText) else searchStr := FindText;
    { Perform a global case-sensitive search for FindText in memoStr }
    FSelPos := Pos(searchStr, memoStr); //FSelPos ist eine globale Variable
    if WholeWord and
       (FSelPos > 1) and (FSelPos+Length(searchStr) <= Length(memoStr)) and
       (not(memoStr[FSelPos-1] in TextDelimeter) or not(memoStr[FSelPos+Length(searchStr)] in TextDelimeter)) then
      FSelPos:=0; //danach wird nichts mehr gefunden!
    if FSelPos > 0 then
    begin
       { Found something, correct position for the location of the start
         of search. }

      FSelPos := FSelPos + StartPos - 1;
      Memo1.SelStart := FSelPos - 1;
      Memo1.SelLength := Length(FindText);
      Memo1.SetFocus;
    end
    else
    begin
      { No joy, show a message. }
      if frfindNext in Options then
        memoStr := ' weiteren'
      else
        memoStr := '';
      MessageDlg(Format('Keine%s Fundstellen von "%s" im Memo.',[memoStr,searchStr]), mtWarning, [mbOK], 0);
    end
  end;
end;
(ursprüngliche Idee aus Swiss-Delphicenter)
So hatte ich mir das vorgestellt.
Willie.
Gut hören kann ich schlecht, schlecht sehen kann ich gut - Ersteres stimmt nicht, das zweite schon.

Geändert von Willie1 (10. Jun 2019 um 17:26 Uhr) Grund: kein
  Mit Zitat antworten Zitat