The Find and Replace Dialogues

The Find Dialog provides the facility to find text in a control such as an edit box.

The example begun in the ColorDialog section and continued in the PrintDialog section is extended by adding a FInd dialog and a Replace Dialog.

The code for the Find button (second from right)

procedure TForm1.ToolButton4Click(Sender: TObject);
var
i, StartPos, ToEnd, FoundAt:integer;
begin
 if Finddialog1.execute then
 with RichEdit1 do
 begin
  { begin the search after the current selection if there is one }
  { otherwise, begin at the start of the text }

 if SelLength <> 0 then
  StartPos := SelStart + SelLength
  else
  StartPos := 0;
   { ToEnd is the length from StartPos to the end of the text in the rich edit control }
  ToEnd := Length(Text) - StartPos;
  FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
  if FoundAt <> -1 then
  begin
   SetFocus;
   SelStart := FoundAt;
   SelLength := Length(FindDialog1.FindText);
  end;
 end;
end;

(Code from the Help section of Delphi 6).

The code for the PrintSetUp button (on the right):

procedure TForm1.ToolButton3Click(Sender: TObject);
begin
 printersetupdialog1.Execute;
end;

That's all that's needed!

Back to Palette List