不好意思,没怎么用过TFindDialog,刚看了一下,它需要自己写Find事件。以下内容转(看对你有什么帮助): 根据djdsz的提示,我查了一下delphi帮助,有这种方式,而且这种方式不需要编写任何代码,只需在Design Time设定好属性即可。 就是ActionList中的Standard Action中的Search中的TSearchFind和TSearchFindNext。 今天晚上测试了一下,果真不需要编码就可以达到效果。 但是我在测试过程中发现,TSearchFind在找到FindDialog.Text字串时,并不能显示光标框选在RichEdit中符合条件的字符串,但是实际上光标已经框选好了在RichEdit中符合条件的字符串,因为当点击【取消】按钮退出TSearchFind.DialogFind窗口后,就显示光标已经框选好了在RichEdit中符合条件的字符串。但是如果在RichEdit中按F3启动TSearchFindNext确可以正常查找和正常实现显示光标已经框选好了在RichEdit中符合条件的字符串。 请问这是怎么回事?另外可以让TSearchFind能正常实现显示光标已经框选好了在RichEdit中符合条件的字符串吗,如何实现? 我的测试方法:在ActionList中新增Standard Action中Search类中的SearchFind1和SearchFindNext1,然后将MainMenu中的【查找】MenuItem的Action指向到SearchFind1;【查找下一个】MenuItem的Action指向到SearchFindNext1;SearchFindNext1的Search Find属性设定为SearchFind1。应该没有什么错误吧?但是在Search text时就是不能显示光标框选在RichEdit中符合条件的字符串。 另外,我还在help中看到了下面这个东东-SearchEdit,它应该可以实现,但是我不知道如何使用这个SearchEdit函数,请指教。{ SearchEdit scans the text of a TCustomEdit-derived component for a given search string. The search starts at the current caret position in the control unless FindFirst is true then the search starts at the beginning. The Options parameter determines whether the search runs forward (frDown) or backward from the caret position, whether or not the text comparison is case sensitive, and whether the matching string must be a whole word. If text is already selected in the control, the search starts at the 'far end' of the selection (SelStart if searching backwards, SelEnd if searching forwards). If a match is found, the control's text selection is changed to select the found text and the function returns True. If no match is found, the function returns False. }function SearchEdit(EditControl: TCustomEdit; const SearchString: String; Options: TFindOptions; FindFirst: Boolean = False): Boolean; var Buffer, P: PChar; Size: Word; SearchOptions: TStringSearchOptions; begin Result := False; if (Length(SearchString) = 0) then Exit; Size := EditControl.GetTextLen; if (Size = 0) then Exit; Buffer := StrAlloc(Size + 1); try SearchOptions := []; if frDown in Options then Include(SearchOptions, soDown); if frMatchCase in Options then Include(SearchOptions, soMatchCase); if frWholeWord in Options then Include(SearchOptions, soWholeWord); EditControl.GetTextBuf(Buffer, Size + 1); if FindFirst then P := SearchBuf(Buffer, Size, 0, EditControl.SelLength, SearchString, SearchOptions) else P := SearchBuf(Buffer, Size, EditControl.SelStart, EditControl.SelLength, SearchString, SearchOptions); if P <> nil then begin EditControl.SelStart := P - Buffer; EditControl.SelLength := Length(SearchString); Result := True; end; finally StrDispose(Buffer); end; end;
不要以为TFindDialog是什么万能的东西。
根据djdsz的提示,我查了一下delphi帮助,有这种方式,而且这种方式不需要编写任何代码,只需在Design Time设定好属性即可。
就是ActionList中的Standard Action中的Search中的TSearchFind和TSearchFindNext。
今天晚上测试了一下,果真不需要编码就可以达到效果。
但是我在测试过程中发现,TSearchFind在找到FindDialog.Text字串时,并不能显示光标框选在RichEdit中符合条件的字符串,但是实际上光标已经框选好了在RichEdit中符合条件的字符串,因为当点击【取消】按钮退出TSearchFind.DialogFind窗口后,就显示光标已经框选好了在RichEdit中符合条件的字符串。但是如果在RichEdit中按F3启动TSearchFindNext确可以正常查找和正常实现显示光标已经框选好了在RichEdit中符合条件的字符串。
请问这是怎么回事?另外可以让TSearchFind能正常实现显示光标已经框选好了在RichEdit中符合条件的字符串吗,如何实现?
我的测试方法:在ActionList中新增Standard Action中Search类中的SearchFind1和SearchFindNext1,然后将MainMenu中的【查找】MenuItem的Action指向到SearchFind1;【查找下一个】MenuItem的Action指向到SearchFindNext1;SearchFindNext1的Search Find属性设定为SearchFind1。应该没有什么错误吧?但是在Search text时就是不能显示光标框选在RichEdit中符合条件的字符串。 另外,我还在help中看到了下面这个东东-SearchEdit,它应该可以实现,但是我不知道如何使用这个SearchEdit函数,请指教。{ SearchEdit scans the text of a TCustomEdit-derived component for a given
search string. The search starts at the current caret position in the
control unless FindFirst is true then the search starts at the beginning.
The Options parameter determines whether the search runs forward
(frDown) or backward from the caret position, whether or not the text
comparison is case sensitive, and whether the matching string must be a
whole word. If text is already selected in the control, the search starts
at the 'far end' of the selection (SelStart if searching backwards, SelEnd
if searching forwards). If a match is found, the control's text selection
is changed to select the found text and the function returns True. If no
match is found, the function returns False. }function SearchEdit(EditControl: TCustomEdit; const SearchString: String;
Options: TFindOptions; FindFirst: Boolean = False): Boolean;
var
Buffer, P: PChar;
Size: Word;
SearchOptions: TStringSearchOptions;
begin
Result := False;
if (Length(SearchString) = 0) then Exit;
Size := EditControl.GetTextLen;
if (Size = 0) then Exit;
Buffer := StrAlloc(Size + 1);
try
SearchOptions := [];
if frDown in Options then
Include(SearchOptions, soDown);
if frMatchCase in Options then
Include(SearchOptions, soMatchCase);
if frWholeWord in Options then
Include(SearchOptions, soWholeWord);
EditControl.GetTextBuf(Buffer, Size + 1);
if FindFirst then
P := SearchBuf(Buffer, Size, 0, EditControl.SelLength,
SearchString, SearchOptions)
else
P := SearchBuf(Buffer, Size, EditControl.SelStart, EditControl.SelLength,
SearchString, SearchOptions);
if P <> nil then
begin
EditControl.SelStart := P - Buffer;
EditControl.SelLength := Length(SearchString);
Result := True;
end;
finally
StrDispose(Buffer);
end;
end;