Insert
ReplaceDialog
component.
Add new
ReplaceText
event (
Object Explorer
->
Events
bookmark)
Paste below code (for RichEdit component):
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
SelPos: Integer;
begin
with TReplaceDialog(Sender) do
begin
SelPos := Pos(FindText, RichEdit1.Lines.Text);
if SelPos > 0 then
begin
RichEdit1.SelStart := SelPos - 1;
RichEdit1.SelLength := Length(FindText);
RichEdit1.SelText := ReplaceText;
end
else MessageDlg(Concat('No more matches for: "', FindText, '" have been found in the current document'), mtError, [mbOk], 0);
end;
end;
Now, insert new button and add OnClick event for it (as the same way you've added it previously) and paste below code:
ReplaceDialog1.Execute;
And finally, add
OnFind
event to the
ReplaceDialog
procedure TForm1.ReplaceDialogFind(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
if SelLength <> 0 then
StartPos := SelStart + SelLength
else
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(ReplaceDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(ReplaceDialog1.FindText);
end;
end;
end;
That's all!
Another way:
How to search and replace text in a RichEdit - Delphi Tips - CJC Delphi[
^]