Difference between revisions of "TFindDialog"
(→Usage: FindNext) |
TyphonFinger (talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{TFindDialog}} | {{TFindDialog}} | ||
− | '''TFindDialog''' [[Image:tfinddialog.png]] is a component that aids in | + | '''TFindDialog''' [[Image:tfinddialog.png]] is a component that aids in searching information. It can be found on the [[Dialogs tab]] of the [[Component Palette]]. |
− | To activate a FindDialog, | + | It is important to know that the dialog itself does not perform a search; it just provides a user interface for all the search parameters. To activate a FindDialog, call its <tt>Execute()</tt> [[Function|function]]. Create an <tt>OnFind</tt> [[Method|method]] in which the search itself is performed. |
[[image:component-TFindDialog.png]] | [[image:component-TFindDialog.png]] | ||
== Usage == | == Usage == | ||
+ | The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo ([[TMemo]]). | ||
<syntaxhighlight lang="pas"> | <syntaxhighlight lang="pas"> | ||
procedure TForm1.Button1Click(Sender: TObject); | procedure TForm1.Button1Click(Sender: TObject); | ||
Line 13: | Line 14: | ||
FindDialog1.Execute(); | FindDialog1.Execute(); | ||
end; | end; | ||
− | |||
procedure TForm1.FindDialog1Find(Sender: TObject); | procedure TForm1.FindDialog1Find(Sender: TObject); | ||
Line 24: | Line 24: | ||
Memo1.SelStart := k - 1; | Memo1.SelStart := k - 1; | ||
Memo1.SelLength := Length(FindText); | Memo1.SelLength := Length(FindText); | ||
+ | Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed | ||
end else | end else | ||
Beep(); | Beep(); | ||
Line 30: | Line 31: | ||
</syntaxhighlight> | </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: | + | 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|unit]] StrUtils is perfect for this purpose: |
<syntaxhighlight lang="pas"> | <syntaxhighlight lang="pas"> | ||
Line 63: | Line 64: | ||
Memo1.SelStart := FFoundPos - 1; | Memo1.SelStart := FFoundPos - 1; | ||
Memo1.SelLength := Length(FindText); | Memo1.SelLength := Length(FindText); | ||
+ | Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed | ||
end else | end else | ||
Beep(); | Beep(); |
Latest revision as of 08:38, 1 May 2020
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
中文(中国大陆) (zh_CN) │
TFindDialog is a component that aids in searching information. It can be found on the Dialogs tab of the Component Palette.
It is important to know that the dialog itself does not perform a search; it just provides a user interface for all the search parameters. To activate a FindDialog, call its Execute() function. Create an OnFind method in which the search itself is performed.
Usage
The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo (TMemo).
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);
Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
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);
Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
end else
Beep();
end;
end;
See also