1

I've implemented a search using the TFindDialog on my form. Everything works well except that I cannot find a way to mimic the "F3 - Find Next" behaviour as in Notepad. Once you have entered a search string, pressing F3 finds the next instance without opening the search dialog.

Regards, Pieter.

Pieter van Wyk
  • 2,316
  • 9
  • 48
  • 65
  • 1
    I'm a bit confused. You have coded the F3 handling yourself, right? So it's up to you whether the dialog is shown. – Uli Gerhardt May 19 '10 at 12:58
  • Sorry if it is unclear. I have done the initial part to start the search using the TFindDialog. I would like to continue a search by pressing a key such as F3, to continue the search without re-opening the SearchDialog form. – Pieter van Wyk May 19 '10 at 13:59

2 Answers2

1

Here's a sketch how one could do this:

type
  TForm1 = class(TForm)
    FindDialog1: TFindDialog;
    procedure FindDialog1Find(Sender: TObject);
    procedure SearchFind1Execute(Sender: TObject);
    procedure SearchFindNext1Execute(Sender: TObject);
  private
    FSearchText: string;
    procedure Search;
  end;

and

procedure TForm1.Search;
begin
  // Do the real searching here...
  MessageBox(Handle, PChar('Looking for "' + FSearchText + '".'), nil, 0);
end;

procedure TForm1.SearchFind1Execute(Sender: TObject);
begin
  // Triggered by Ctrl-F
  FindDialog1.FindText := FSearchText;
  FindDialog1.Execute;
end;

procedure TForm1.SearchFindNext1Execute(Sender: TObject);
begin
  // Triggered by F3
  if FSearchText = '' then
    SearchFind1.Execute
  else
    Search;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
begin
  // Triggered by button click in FindDialog1
  FSearchText := FindDialog1.FindText;
  Search;
end;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • That works. I only needed to call the 'Find' method of the FindDialog again. I also need to set 'FindDialog1.Options := FindDialog1.Options + [frFindNext];'. Thank you. – Pieter van Wyk May 19 '10 at 14:51
  • 1
    `Find` is protected (at least in D2007). How do you call it? Maybe it's a better idea to factor out the "real" search code into a method that you call both in your OnFind handler and the F3 click handler. Then there's no need to mess with frFindNext etc. (Disclaimer: all untested :-)) – Uli Gerhardt May 19 '10 at 14:57
  • The FindDialog has a OnFind method. That is where all the code goes to do the search. – Pieter van Wyk May 20 '10 at 08:10
0

Alternativaly you could try the standard actions TSearchFind/TSearchFindNext. However I haven't tried them myself, so I can't say how well they work in practice.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83