TFindDialog/zh CN: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TFindDialog/zh CN
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TFindDialog**  是搜索的弹出对话框. 在 [[Dialogs tab]] 的 [[Component Palette]]. | |
| 2 | + | ||
| 3 | + | 对话框本身不会搜索; 仅仅是为搜索参数提供一个界面而已. 激活搜索框, 使用方法:`Execute()` [[Function|function]]. 创建 `OnFind` [[Method|method]] 然后咋其中搜索. | |
| 4 | + | ||
| 5 | + | <figure> | |
| 6 | + | <img src="component-TFindDialog.png" title="component-TFindDialog.png" /> | |
| 7 | + | <figcaption>component-TFindDialog.png</figcaption> | |
| 8 | + | </figure> | |
| 9 | + | ||
| 10 | + | ## 用法 | |
| 11 | + | ||
| 12 | + | 如下示例显示了使用TFindDialog在([[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; //必须激活控件,否则不显示选择效果 | |
| 30 | + | end else | |
| 31 | + | Beep(); | |
| 32 | + | end; | |
| 33 | + | end; | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | 如果要继续搜索同样的内容,必须记住最后一次搜索的位置,从此处继续搜索。 [[Unit|unit]] 中的`PosEx()` 就是用来标记位置的: | |
| 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; //必须激活控件,否则不显示选择效果 | |
| 70 | + | end else | |
| 71 | + | Beep(); | |
| 72 | + | end; | |
| 73 | + | end; | |
| 74 | + | ``` | |
| 75 | + | ||
| 76 | + | ## 参考资料 | |
| 77 | + | ||
| 78 | + | - [[doc:lcl/dialogs/tfinddialog.html|TFindDialog doc]] | |
| 79 | + | - [[TReplaceDialog]] | |
| 80 | + | ||
| 81 | + | --- | |
| 82 | + | *From the Free Pascal wiki, page "TFindDialog/zh CN", 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).* |
