FIND AND REPLACE - CREATING NOTEPAD
Improving MyNotepad with Find and Replace dialog boxes. Part 2.
In the previous article we saw how to create a simple MyNotepad application with the file Open and Save capabilities. It is quite obvious that MyNotepad is a very simple application that can hardly substitute Windows Notepad. One of the most important features of Windows Notepad is the ability to allow users to search for some text and than modify that text.
Therefore, this time we'll further extend our project with search and replace commands.
TFindDialog
TReplaceDialog ![]()
The TFindDialog and TReplaceDialog components display a modeless Windows dialog box that prompts the user for a search (and replace) string, with a variety of search and replace options.

The Execute method for the FindDialog and ReplaceDialog components is a little different than it is with the Open and Save dialogs. As soon as the dialog box is displayed, the Execute method returns true (Find and Replace dialog boxes are modeless dialog boxes.) To close a dialog, we call its CloseDialog method.
Note: Both Find and Replace dialog boxes can be found on the Dialogs tab of the Component palette. To use a common dialog box, we need to place the appropriate component (components) anywhere on the form.
OnFind and OnReplace
The Find and Replace dialog boxes use the OnFind and OnReplace events along with the Options property to determine what is happening with the dialog box.
The code inside an OnFind event handler should search for the text specified in FindText property, using the Options flags to determine how the search is conducted. Similarly, code inside an OnReplace event handler of TReplaceDialog box should find the text specified in FindText and replaces it with the text in ReplaceText.
MyNotepad - search and replace
Let's draw & code:
1. Start Delphi and open our MyNotepad project.
2. Add two buttons on a form; name them btnFind and btnReplace.
3. Place TFindDialog and TReplaceDialog on the form.

Use Object Inspector to assign the following code to both btnFind and btnReplace OnClick event handlers:
procedure TForm1.btnFindClick(Sender: TObject); begin FindDialog1.Execute; end; ... procedure TForm1.btnReplaceClick(Sender: TObject); begin ReplaceDialog1.Execute; end; |
Next, add the following code to the OnReplace event handler of TReplaceDialog control. The OnFind event handler of the TFindDialog control only lacks the Memo1.SelText := ReplaceText line.
procedure TForm1.ReplaceDialog1Replace
(Sender: TObject);
var
SelPos, SPos, SLen, TextLength: Integer;
SearchString : string;
begin
with TReplaceDialog(Sender) do begin
TextLength:=Length(Memo1.Lines.Text);
SPos:=Memo1.SelStart;
SLen:=Memo1.SelLength;
SearchString := Copy(Memo1.Lines.Text,
SPos + SLen + 1,
TextLength - SLen + 1);
SelPos := Pos(FindText, SearchString);
if SelPos > 0 then begin
Memo1.SelStart := (SelPos - 1) + (SPos + SLen);
Memo1.SelLength := Length(FindText);
{remove this in the OnFind procedure:}
Memo1.SelText := ReplaceText;
end
else MessageDlg('Could not find "' + FindText +
'" in Memo1.', mtError, [mbOk], 0);
end;
end;
|
That's it, now we can search for text and replace the text found with our Notepad. However, due to the large amount of search and replace options this code is not "finished". It will only search and replace text in Memo1 when the search direction is down (frDown in Options).
Next article will show how to further improve MyNotepad with menus.
