Try the attached simple example you can adapt and extend.
On a new project form named Unit1 drop a memo named MText, a labeled edit named EWordToFind, a button named BFind and a TFindDialog named FindDialog1. Double-click the button to create an OnClick handler for it, and complete the code as follows:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Forms, Dialogs, StdCtrls, ExtCtrls, strutils, sysutils;
type
{ TForm1 }
TForm1 = class(TForm)
BFind: TButton;
EWordToFind: TLabeledEdit;
FindDialog1: TFindDialog;
MText: TMemo;
procedure BFindClick(Sender: TObject);
procedure FindDialog1Find(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.BFindClick(Sender: TObject);
begin
EWordToFind.Text:= 'the';
MText.Lines.Text:= 'memText contains some example text' +
'to serve as a test for the Lazarus' +
'TFindDialog. It also contains a few' +
'repeated words, i.e. words that appear' +
'more than once in the same text.';
FindDialog1.Options:= FindDialog1.Options + [frWholeWord, frHideEntireScope]; // adapt as required
FindDialog1.FindText:= EWordToFind.Text;
MText.SelStart:=0;
MText.HideSelection:=False;
FindDialog1.Execute;
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
var txtToSearch, txtToFind: string;
p: integer;
begin
if (frDown in FindDialog1.Options) then
begin
txtToSearch := MText.Text;
txtToFind:=FindDialog1.FindText;
end
else
begin
txtToSearch := ReverseString(MText.Text);
txtToFind:= ReverseString(FindDialog1.FindText);
end;
if (frMatchCase in FindDialog1.Options)
then p := PosEx(txtToFind, txtToSearch)
else p := PosEx(Uppercase(txtToFind), Uppercase(txtToSearch));
case (p>0) of
False: begin
ShowMessageFmt('The text "%s" was NOT found',[FindDialog1.FindText]);
MText.SelLength:=0;
end;
True: begin
if (frDown in FindDialog1.Options)
then MText.SelStart:= Pred(p)
else MText.SelStart:= Length(MText.Text) - p - Pred(Length(txtToFind));
MText.SelLength:= Length(txtToFind);
end;
end;
end;
end.