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