procedure TForm1.Edit1Change(Sender: TObject);   //输入 时 实时 筛选
var i:integer;
begin
ListBox2.Visible:=false;
ListBox2.Items.Clear;
  for i := 0 to aStringList.Count -1 do begin
    if pos( Edit1.Text, aStringList[i])>0 then   begin

      ListBox2.Items.Add(aStringList[i])
    end;

  end;
ListBox2.Visible:=ListBox2.Items.Count>0;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if ( key=VK_DOWN) and (listbox2.Items.Count>0)   then begin //按 向下键,选择
 ListBox2.SetFocus;
 ListBox2.ItemIndex:=0;
end;


end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin  // 按回车键,Edit内容 添加到 历史记录里
   aStringList.Add(Edit1.Text)
end

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
aStringList.SaveToFile('History.txt'); //退出时,将历史记录保存到 硬盘里
  aStringList.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   aStringList:= tStringList.Create;

aStringList.sorted := True;          //历史记录 去除功能
aStringList.Duplicates := dupIgnore;

//   aStringList.Add('aa');
//   aStringList.Add('ab');
//   aStringList.Add('cc');
//   aStringList.Add('bc');
//   aStringList.Add('dd');
aStringList.LoadFromFile('History.txt');  //启动时,加载历史记录
end;

procedure TForm1.ListBox2KeyPress(Sender: TObject; var Key: Char);
begin
if (key=#13) and (listbox2.Items.Count>0) then begin //选择 历史记录 项
   Edit1.Text:= listbox2.Items[listbox2.ItemIndex];
   edit1.SetFocus;
   listbox2.Visible:=false;
end
end;

end.

 就像Chrome网址输入栏一样记录历史网址一样


1[输入时,若历史记录中有,则实时带出,模糊记录(部分包含),按 向下键,可以选择历史 项,

2]若历史记录没有,按回车,则加入到历史记录中,下次 输入则会带出来


  添加功能,模糊带出时,突出模糊字符串

0]要将ListBox的Style设为lbOwnerDrawFixed

调整ListBox2的字体大小后,记得调整它的ItemHeight


增加大小写不敏感查询

procedure TForm1.ListBox2DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var aX:integer;
   leftStr,midStr,rightStr,yyyStr:string;
begin

ax := Rect.Left ;
midStr:=lowercase(edit1.Text);
 ListBox2.Canvas.Font.Color := clblack;
    leftStr:=copy(
                   ListBox2.Items[index]
                   ,1
                   ,pos(midStr, lowercase(ListBox2.Items[index]))-1);   //  左 文字
  ListBox2.Canvas.TextOut(ax  , Rect.Top ,leftStr);

  Inc(ax, ListBox2.Canvas.TextWidth(leftStr));
  ax:=ax+1;
//=====================================================================================
 ListBox2.Canvas.Font.Color := clred;

       //  中 文字
    yyyStr:=copy(
                    ListBox2.Items[index]
                    , pos(midStr, lowercase(ListBox2.Items[index]))
                    , length(midStr)
                 );
  ListBox2.Canvas.TextOut(ax , Rect.Top ,yyyStr);
  Inc(ax, ListBox2.Canvas.TextWidth(yyyStr));
  ax:=ax+1;
//=====================================================================================
 ListBox2.Canvas.Font.Color := clblack;
    rightStr:=copy(
                     ListBox2.Items[index]
                                 ,pos(midStr, lowercase(ListBox2.Items[index]))+length(midStr)
                                 ,100 );      //  右 文字
  ListBox2.Canvas.TextOut(ax , Rect.Top ,rightStr);

end;

链接:https://pan.baidu.com/s/1cST-NSoy2ZFh-FQ44L3XxA
提取码:rgca