Difference between revisions of "TFindDialog"
From Free Pascal wiki
Jump to navigationJump to search (→Usage: FindNext) |
|||
Line 8: | Line 8: | ||
== Usage == | == Usage == | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pas"> |
− | procedure | + | procedure TForm1.Button1Click(Sender: TObject); |
begin | begin | ||
FindDialog1.Execute(); | FindDialog1.Execute(); | ||
Line 15: | Line 15: | ||
− | procedure | + | procedure TForm1.FindDialog1Find(Sender: TObject); |
var | var | ||
k: integer; | k: integer; | ||
begin | begin | ||
with Sender as TFindDialog do begin | with Sender as TFindDialog do begin | ||
− | k := Pos( FindText, Memo1.Lines.Text ); | + | k := Pos(FindText, Memo1.Lines.Text); |
− | if k>0 then begin | + | if k > 0 then begin |
− | Memo1. | + | Memo1.SelStart := k - 1; |
− | Memo1.SelLength := | + | Memo1.SelLength := Length(FindText); |
+ | end else | ||
+ | Beep(); | ||
+ | end; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | If you want to repeat the operation several times with the same search text you must remember the last found position and continue searching from here - the function <tt>PosEx()</tt> in unit StrUtils is perfect for this purpose: | ||
+ | |||
+ | <syntaxhighlight lang="pas"> | ||
+ | uses | ||
+ | StrUtils; | ||
+ | |||
+ | type | ||
+ | TForm1 = class(TForm) | ||
+ | private | ||
+ | FFoundPos: Integer; | ||
+ | ... | ||
+ | |||
+ | procedure TForm1.Button1Click(Sender: TObject); | ||
+ | begin | ||
+ | with FindDialog1 do | ||
+ | begin | ||
+ | if frEntireScope in Options then // Search begins at file start | ||
+ | FFoundPos := 0 | ||
+ | else | ||
+ | FFoundPos := Memo1.SelStart; // Serach begins at current cursor position | ||
+ | Execute; | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.FindDialog1Find(Sender: TObject); | ||
+ | begin | ||
+ | with Sender as TFindDialog do | ||
+ | begin | ||
+ | FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1); | ||
+ | if FFoundPos > 0 then | ||
+ | begin | ||
+ | Memo1.SelStart := FFoundPos - 1; | ||
+ | Memo1.SelLength := Length(FindText); | ||
end else | end else | ||
Beep(); | Beep(); |
Revision as of 19:29, 6 October 2019
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
中文(中国大陆) (zh_CN) │
TFindDialog is a component that aids in finding information. It can be found on the Dialogs tab of the Component Palette.
To activate a FindDialog, just call its Execute() function. Create an OnFind method to perform the actual find.
Usage
procedure TForm1.Button1Click(Sender: TObject);
begin
FindDialog1.Execute();
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
var
k: integer;
begin
with Sender as TFindDialog do begin
k := Pos(FindText, Memo1.Lines.Text);
if k > 0 then begin
Memo1.SelStart := k - 1;
Memo1.SelLength := Length(FindText);
end else
Beep();
end;
end;
If you want to repeat the operation several times with the same search text you must remember the last found position and continue searching from here - the function PosEx() in unit StrUtils is perfect for this purpose:
uses
StrUtils;
type
TForm1 = class(TForm)
private
FFoundPos: Integer;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
with FindDialog1 do
begin
if frEntireScope in Options then // Search begins at file start
FFoundPos := 0
else
FFoundPos := Memo1.SelStart; // Serach begins at current cursor position
Execute;
end;
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
begin
with Sender as TFindDialog do
begin
FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1);
if FFoundPos > 0 then
begin
Memo1.SelStart := FFoundPos - 1;
Memo1.SelLength := Length(FindText);
end else
Beep();
end;
end;
See also