TFindDialog: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TFindDialog
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TFindDialog**  is a component that aids in searching information. It can be found on the [[Dialogs tab]] of the [[Component Palette]]. | |
| 2 | + | ||
| 3 | + | 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|function]]. Create an `OnFind` [[Method|method]] in which the search itself is performed. | |
| 4 | + | ||
| 5 | + | <figure> | |
| 6 | + | <img src="component-TFindDialog.png" title="component-TFindDialog.png" /> | |
| 7 | + | <figcaption>component-TFindDialog.png</figcaption> | |
| 8 | + | </figure> | |
| 9 | + | ||
| 10 | + | ## Usage | |
| 11 | + | ||
| 12 | + | The following examples illustrate usage of TFindDialog for search a specific phrase within the text stored in a memo ([[TMemo]]). | |
| 13 | + | ||
| 14 | + | ``` pas | |
| 15 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 16 | + | begin | |
| 17 | + | FindDialog1.Execute(); | |
| 18 | + | end; | |
| 19 | + | ||
| 20 | + | procedure TForm1.FindDialog1Find(Sender: TObject); | |
| 21 | + | var | |
| 22 | + | k: integer; | |
| 23 | + | begin | |
| 24 | + | with Sender as TFindDialog do begin | |
| 25 | + | k := Pos(FindText, Memo1.Lines.Text); | |
| 26 | + | if k > 0 then begin | |
| 27 | + | Memo1.SelStart := k - 1; | |
| 28 | + | Memo1.SelLength := Length(FindText); | |
| 29 | + | Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed | |
| 30 | + | end else | |
| 31 | + | Beep(); | |
| 32 | + | end; | |
| 33 | + | end; | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | 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|unit]] StrUtils is perfect for this purpose: | |
| 37 | + | ||
| 38 | + | ``` pas | |
| 39 | + | uses | |
| 40 | + | StrUtils; | |
| 41 | + | ||
| 42 | + | type | |
| 43 | + | TForm1 = class(TForm) | |
| 44 | + | private | |
| 45 | + | FFoundPos: Integer; | |
| 46 | + | ... | |
| 47 | + | ||
| 48 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 49 | + | begin | |
| 50 | + | with FindDialog1 do | |
| 51 | + | begin | |
| 52 | + | if frEntireScope in Options then // Search begins at file start | |
| 53 | + | FFoundPos := 0 | |
| 54 | + | else | |
| 55 | + | FFoundPos := Memo1.SelStart; // Serach begins at current cursor position | |
| 56 | + | Execute; | |
| 57 | + | end; | |
| 58 | + | end; | |
| 59 | + | ||
| 60 | + | procedure TForm1.FindDialog1Find(Sender: TObject); | |
| 61 | + | begin | |
| 62 | + | with Sender as TFindDialog do | |
| 63 | + | begin | |
| 64 | + | FFoundPos := PosEx(FindText, Memo1.Lines.Text, FFoundPos+1); | |
| 65 | + | if FFoundPos > 0 then | |
| 66 | + | begin | |
| 67 | + | Memo1.SelStart := FFoundPos - 1; | |
| 68 | + | Memo1.SelLength := Length(FindText); | |
| 69 | + | Memo1.SetFocus; // Memo1 must be activated, otherwise the selection effect will not be displayed | |
| 70 | + | end else | |
| 71 | + | Beep(); | |
| 72 | + | end; | |
| 73 | + | end; | |
| 74 | + | ``` | |
| 75 | + | ||
| 76 | + | ## See also | |
| 77 | + | ||
| 78 | + | - [[doc:lcl/dialogs/tfinddialog.html|TFindDialog doc]] | |
| 79 | + | - [[TReplaceDialog]] | |
| 80 | + | ||
| 81 | + | --- | |
| 82 | + | *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).* |
