Skip to main content
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.

Create a Find DialogPascalC++
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;
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.

Handle Find EventsPascalC++
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;
void __fastcall TForm1::TMSFNCFindDialog1Find(TObject *Sender, System::UnicodeString AFindText, bool &AFound)
{
  AFound = Pos(AFindText, Memo1->Text) > 0;
}

void __fastcall TForm1::TMSFNCFindDialog1FindNext(TObject *Sender, System::UnicodeString AFindText, bool &AFound)
{
  AFound = Pos(AFindText, Memo1->Text.SubString(Memo1->SelStart + Memo1->SelLength + 1, MaxInt)) > 0;
}

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.

Use the Extended ViewPascalC++
TMSFNCFindDialog1.Options := TMSFNCFindDialog1.Options + [fdoMoreEnabled, fdoMoreExpanded];
TMSFNCFindDialog1.VisibleOptions := TMSFNCFindDialog1.VisibleOptions + [fdovMore, fdovFindMemo];
TMSFNCFindDialog1.VisibleOptions := TMSFNCFindDialog1.VisibleOptions - [fdovContinueToNextFile];
TMSFNCFindDialog1.OnSetMarker := TMSFNCFindDialog1SetMarker;
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:

Combining search history, find events, and extended viewPascalC++
procedure TForm1.FormCreate(Sender: TObject);
begin
  { Pre-populate history from the last session. FindList is a TStringList. }
  TMSFNCFindDialog1.FindList.AddStrings(FRecentSearches);
  TMSFNCFindDialog1.AutoHistory := True;

  { Two separate sets: Options holds behaviour flags (TTMSFNCFindDialogOption)
    and VisibleOptions decides which controls are shown
    (TTMSFNCFindDialogVisibleOption). fdoMore* and fdovMore are NOT
    interchangeable - each belongs to its own set. }
  TMSFNCFindDialog1.Options := TMSFNCFindDialog1.Options
    + [fdoMoreEnabled, fdoMoreExpanded];
  TMSFNCFindDialog1.VisibleOptions := TMSFNCFindDialog1.VisibleOptions
    + [fdovMore];
end;

{ OnFind and OnFindNext share one signature, TTMSFNCFindDialogFindEvent: the
  search flags are not passed as a parameter - read them from Options. }
procedure TForm1.TMSFNCFindDialog1Find(Sender: TObject; AFindText: string;
  var AFound: Boolean);
begin
  AFound := DoSearch(AFindText,
    fdoCaseSensitive in TMSFNCFindDialog1.Options,
    fdoWholeWordOnly in TMSFNCFindDialog1.Options,
    True { start from the top });
end;

procedure TForm1.TMSFNCFindDialog1FindNext(Sender: TObject; AFindText: string;
  var AFound: Boolean);
begin
  { Direction is a flag too: fdoDown is set when the user searches forwards. }
  AFound := DoSearch(AFindText,
    fdoCaseSensitive in TMSFNCFindDialog1.Options,
    fdoWholeWordOnly in TMSFNCFindDialog1.Options,
    False { continue from the current position });
end;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Pre-populate history from the last session. FindList is a TStringList.
  TMSFNCFindDialog1->FindList->AddStrings(FRecentSearches);
  TMSFNCFindDialog1->AutoHistory = true;

  // Two separate sets: Options holds behaviour flags (TTMSFNCFindDialogOption)
  // and VisibleOptions decides which controls are shown
  // (TTMSFNCFindDialogVisibleOption). fdoMore* and fdovMore are NOT
  // interchangeable - each belongs to its own set.
  TMSFNCFindDialog1->Options = TMSFNCFindDialog1->Options
    << fdoMoreEnabled << fdoMoreExpanded;
  TMSFNCFindDialog1->VisibleOptions = TMSFNCFindDialog1->VisibleOptions
    << fdovMore;
}

// OnFind and OnFindNext share one signature, TTMSFNCFindDialogFindEvent: the
// search flags are not passed as a parameter - read them from Options.
void __fastcall TForm1::TMSFNCFindDialog1Find(TObject *Sender, System::UnicodeString AFindText,
  bool &AFound)
{
  AFound = DoSearch(AFindText,
    TMSFNCFindDialog1->Options.Contains(fdoCaseSensitive),
    TMSFNCFindDialog1->Options.Contains(fdoWholeWordOnly),
    true /* start from the top */);
}

void __fastcall TForm1::TMSFNCFindDialog1FindNext(TObject *Sender, System::UnicodeString AFindText,
  bool &AFound)
{
  // Direction is a flag too: fdoDown is set when the user searches forwards.
  AFound = DoSearch(AFindText,
    TMSFNCFindDialog1->Options.Contains(fdoCaseSensitive),
    TMSFNCFindDialog1->Options.Contains(fdoWholeWordOnly),
    false /* continue from the current position */);
}

See Also