本文介绍了在记事本中搜索备忘录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能给我一些简单的代码,使我能够搜索备忘录中的简单字符串,并在找到后在备忘录中突出显示吗?

Can anybody give me some simple code that would give me the ability to search a simple string in a memo and have it highlighted in the memo after being found?

推荐答案

此搜索可用于文档换行,区分大小写(不区分大小写)和从光标位置进行搜索。

This search allows for document wrap, case (in)sensitive search and searching from cursor position.

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以显示选择,即使该备忘录没有被聚焦

You can set HideSelection = False on the memo to show the selection even if the memo isn't focussed.

使用如下:

  SearchText(Memo1, Edit1.Text, []);

也允许搜索编辑内容。

这篇关于在记事本中搜索备忘录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 01:08