AdvMemo / AdvMemoFind[Replace]Dialog Next

How do you (programmatically) do a "Find Next" or "Replace Next" using the AdvMemoFind[Replace]Dialog components?

It should do a Find/Replace (with the current options) from SelectPos+SelectLength and on without displaying the dialog box.

You need to perform this at TAdvMemo programmatically via the AdvMemo1.FindAndReplace function.
You can look at the following code coming directly out of the TAdvMemoFindReplaceDialog class:

procedure TAdvMemoFindReplaceDialog.ReplaceAll(Sender: TObject);
var
  AReplaceText, AFindText: string;
  Options: TFindOptionsEx;
begin
  if not Assigned(FAdvMemo) then
    Exit;

  if Assigned(OnReplaceAllText) then
    OnReplaceAllText(Self);

  Options := [freDown, freReplaceAll];

  AReplaceText := FReplaceDialogEx.ReplaceText;
  AFindText := FReplaceDialogEx.FindText;

  if fdoCaseSensitive in FReplaceDialogEx.Options then
    Options := Options + [freMatchCase];

  if fdoWholeWordOnly in FReplaceDialogEx.Options then
    Options := Options + [freWholeWord];

  if fdoSelection in FReplaceDialogEx.Options then
    Options := Options + [freSelection];

  if fdoExpression in FReplaceDialogEx.Options then
    Options := Options + [freExpression];

  if fdoWrapAtEndOfFile in FReplaceDialogEx.Options then
    Options := Options + [freWrapAtEnd];

  if not (fdoDown in FReplaceDialogEx.Options) then
    Options := Options - [freDown];

  FCount := FAdvMemo.FindAndReplace(AFindText, AReplaceText, Options);

  DoReplaceDone;
end;

Yikes! Perhaps that could be implemented as a standard method on the TAdvMemoFind[Replace]Dialog. I'll implement it as a CLASS HELPER for now :slight_smile:

(And then I would code "Options := Options + [fre...]" as "Include(Options,fre...)" for optimisation's sake :slight_smile:)

I'm not sure what exactly you mean with wanting to do something programmatically with a dialog?
A dialog is for having a UI, so, what is the need for this dialog to be involved in doing something programmatically?

Consider NotePad. You load a file and press Ctrl+F, enter a search text and click "Next" then close the dialog. You then edit the file around the place your found, and then want to find the next occurrence. You then press F3 and the cursor moves to the next location without showing the Find dialog.

I want to emulate this paradigm in my program...

What code should I place at the F3 key handler to find the next occurrence when using the TAdvMemoFind[Replace]Dialogs?

To find next instances of text searched by the TAdvMemoFind(Replace)Dialog with the F3 key, use something like:

procedure TForm1.AdvMemo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_f3 then
  begin
    advmemo1.FindText(AdvMemoFindDialog1.FindText, [freDown, freFindNext]);
  end;

end;

But what if AdvMemoFindDialog1.Options = [...fdoWholeWordOnly...] or some other options (fdoCaseSensitive, f.ex.) in the dialog. Shouldn't it be a call on the Dialog (with attached AdvMemo) that I should call in order to ensure that all the options are obeyed as it would if the dialog was used visibly?

How do I ensure that all options of the current Find/Replace is obeyed?

I could make a long list like the one that Pieter showed, but that would not be future-proof, whereas a "FindNext" method on the Dialog component would ensure that any changes you may make to the dialog options in the future would follow along...

Good, to save you from this code, we will expose FindNext/ReplaceNext public methods on these dialogs.