Sign in Join
Wiki copytemporary

TFindDialog: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**TFindDialog** ![](/wiki-images/9/9e/Tfinddialog.png) is a component that aids in searching information. It can be found on the [[Dialogs tab]] of the [[Component Palette]].
22
33It 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|function]]. Create an `OnFind` [[Method|method]] in which the search itself is performed.
44
5-<figure>
6-<img src="component-TFindDialog.png" title="component-TFindDialog.png" />
7-<figcaption>component-TFindDialog.png</figcaption>
8-</figure>
5+![](/wiki-images/3/33/Component-TFindDialog.png)
96
107## Usage
118
129The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo ([[TMemo]]).
1310
1411``` pas
1512procedure TForm1.Button1Click(Sender: TObject);
1613begin
1714 FindDialog1.Execute();
1815end;
1916
2017procedure TForm1.FindDialog1Find(Sender: TObject);
2118var
2219 k: integer;
2320begin
2421 with Sender as TFindDialog do begin
2522 k := Pos(FindText, Memo1.Lines.Text);
2623 if k > 0 then begin
2724 Memo1.SelStart := k - 1;
2825 Memo1.SelLength := Length(FindText);
2926 Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
3027 end else
3128 Beep();
3229 end;
3330end;
3431```
3532
3633If 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|unit]] StrUtils is perfect for this purpose:
3734
3835``` pas
3936uses
4037 StrUtils;
4138
4239type
4340 TForm1 = class(TForm)
4441 private
4542 FFoundPos: Integer;
4643 ...
4744
4845procedure TForm1.Button1Click(Sender: TObject);
4946begin
5047 with FindDialog1 do
5148 begin
5249 if frEntireScope in Options then // Search begins at file start
5350 FFoundPos := 0
5451 else
5552 FFoundPos := Memo1.SelStart; // Serach begins at current cursor position
5653 Execute;
5754 end;
5855end;
5956
6057procedure TForm1.FindDialog1Find(Sender: TObject);
6158begin
6259 with Sender as TFindDialog do
6360 begin
6461 FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1);
6562 if FFoundPos > 0 then
6663 begin
6764 Memo1.SelStart := FFoundPos - 1;
6865 Memo1.SelLength := Length(FindText);
6966 Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed
7067 end else
7168 Beep();
7269 end;
7370end;
7471```
7572
7673## See also
7774
7875- [[doc:lcl/dialogs/tfinddialog.html|TFindDialog doc]]
7976- [[TReplaceDialog]]
8077
8178---
8279*From the Free Pascal wiki, page "TFindDialog", as it stood on 2020-05-01 after a change by TyphonFinger, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history