Table of Contents

Search Dialog Workflow

TTMSFNCFindDialog displays a reusable search dialog with history, direction, matching options, and command buttons. The dialog reports search requests through events so the application keeps control over the actual search algorithm.

Configure Search State

Use FindText for the initial search value and FindList for reusable history. Enable AutoHistory when the dialog should append successful search text automatically.

TMSFNCFindDialog1.FindText := 'customer';
TMSFNCFindDialog1.FindList.Clear;
TMSFNCFindDialog1.FindList.Add('customer');
TMSFNCFindDialog1.FindList.Add('invoice');
TMSFNCFindDialog1.AutoHistory := True;
TMSFNCFindDialog1.Options := TMSFNCFindDialog1.Options + [fdoCaseSensitive, fdoCloseIfFound];
TMSFNCFindDialog1.OnFind := TMSFNCFindDialog1Find;
TMSFNCFindDialog1.OnFindNext := TMSFNCFindDialog1FindNext;
TMSFNCFindDialog1.Execute;

Handle Find Events

Handle OnFind and OnFindNext to run the search. Set AFound to True when a match was found; if fdoCloseIfFound is enabled, the dialog can close automatically after a successful search.

procedure TForm1.TMSFNCFindDialog1Find(Sender: TObject; AFindText: string; var AFound: Boolean);
begin
  AFound := Pos(AFindText, Memo1.Text) > 0;
end;

procedure TForm1.TMSFNCFindDialog1FindNext(Sender: TObject; AFindText: string; var AFound: Boolean);
begin
  AFound := Pos(AFindText, Copy(Memo1.Text, Memo1.SelStart + Memo1.SelLength + 1, MaxInt)) > 0;
end;

Use the Extended View

Enable fdoMoreEnabled and fdovMore to expose the More button. Add fdoMoreExpanded when the multi-line search field should be visible as soon as the dialog opens.

TMSFNCFindDialog1.Options := TMSFNCFindDialog1.Options + [fdoMoreEnabled, fdoMoreExpanded];
TMSFNCFindDialog1.VisibleOptions := TMSFNCFindDialog1.VisibleOptions + [fdovMore, fdovFindMemo];
TMSFNCFindDialog1.VisibleOptions := TMSFNCFindDialog1.VisibleOptions - [fdovContinueToNextFile];
TMSFNCFindDialog1.OnSetMarker := TMSFNCFindDialog1SetMarker;

Combining search history, find events, and extended view

The following example pre-loads recent searches, handles both find and find-next, and enables the extended multi-line search view by default:

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Pre-populate history from the last session
  TMSFNCFindDialog1.FindList.AddStrings(FRecentSearches);
  TMSFNCFindDialog1.AutoHistory  := True;
  // Show extended view on open
  TMSFNCFindDialog1.Options := TMSFNCFindDialog1.Options
    + [fdoMoreEnabled, fdovMore, fdoMoreExpanded];
end;

procedure TForm1.TMSFNCFindDialog1Find(Sender: TObject;
  ASearchText: string; AOptions: TTMSFNCFindOptions; var AFound: Boolean);
begin
  AFound := DoSearch(ASearchText, AOptions, True {from top});
end;

procedure TForm1.TMSFNCFindDialog1FindNext(Sender: TObject;
  ASearchText: string; AOptions: TTMSFNCFindOptions; ADirection: TFindDirection;
  var AFound: Boolean);
begin
  AFound := DoSearch(ASearchText, AOptions, False {continue});
end;

See Also