谁能给我一些简单的代码,让我能够在备忘录中搜索简单的字符串,并在被找到后在备忘录中突出显示它?
发布于 2010-11-20 21:03:33
此搜索允许文档换行、区分大小写的搜索以及从光标位置进行搜索。
type
TSearchOption = (soIgnoreCase, soFromStart, soWrap);
TSearchOptions = set of TSearchOption;
function SearchText(
Control: TCustomEdit;
Search: string;
SearchOptions: TSearchOptions): Boolean;
var
Text: string;
Index: Integer;
begin
if soIgnoreCase in SearchOptions then
begin
Search := UpperCase(Search);
Text := UpperCase(Control.Text);
end
else
Text := Control.Text;
Index := 0;
if not (soFromStart in SearchOptions) then
Index := PosEx(Search, Text,
Control.SelStart + Control.SelLength + 1);
if (Index = 0) and
((soFromStart in SearchOptions) or
(soWrap in SearchOptions)) then
Index := PosEx(Search, Text, 1);
Result := Index > 0;
if Result then
begin
Control.SelStart := Index - 1;
Control.SelLength := Length(Search);
end;
end;您可以在备注上设置HideSelection = False以显示选定内容,即使备注没有被聚焦。
像这样使用:
SearchText(Memo1, Edit1.Text, []);也允许搜索编辑。
发布于 2010-11-20 20:54:20
function TForm1.FindText( const aPatternToFind: String):Boolean;
var
p: Integer;
begin
p := pos(aPatternToFind, Memo1.Text);
Result := (p > 0);
if Result then
begin
Memo1.SelStart := p;
Memo1.SelLength := Length(aPatternToFind);
Memo1.SetFocus; // necessary so highlight is visible
end;
end;如果WordWrap为true,则不会跨行搜索。
https://stackoverflow.com/questions/4232709
复制相似问题