加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

自动附加/完成从文本文件到编辑框delphi

发布时间:2020-12-15 10:08:49 所属栏目:大数据 来源:网络整理
导读:我正在尝试创建一个编辑框,我希望它能够自动附加输入时输入的文本.文本将从文本文件中附加“建议”. 假设我的建议文件有这些: 玛丽莲·梦露 马龙白兰度 迈克·迈尔斯 当我开始在编辑框中输入“M”时,剩下的将出现突出显示(或不显示): “arilyn门罗” 而当
我正在尝试创建一个编辑框,我希望它能够自动附加输入时输入的文本.文本将从文本文件中附加“建议”.

假设我的建议文件有这些:
玛丽莲·梦露
马龙白兰度
迈克·迈尔斯

当我开始在编辑框中输入“M”时,剩下的将出现突出显示(或不显示):
“arilyn门罗”
而当我继续输入“Mi”,那么“ke Myers”就会出现在最后.我希望我为你们做足够的清楚!谢谢你的帮助!

解决方法

您可以使用 TComboBox轻松实现此功能.

按着这些次序 :

  • drop a combobox in your form
  • set the autocomplete property to true
  • set the sorted property to true
  • set the style property to csDropDown
  • in the OnExit event of the combobox add a code like this
const
MaxHistory=200;//max number of items


procedure TForm1.ComboBoxSearchExit(Sender: TObject);
begin
   //check if the text entered exist in the list,if not add to the list
   if (Trim(ComboBoxSearch.Text)<>'') and (ComboBoxSearch.Items.IndexOf(ComboBoxSearch.Text)=-1) then 
   begin
     if ComboBoxSearch.Items.Count=MaxHistory then
     ComboBoxSearch.Items.Delete(ComboBoxSearch.Items.Count-1);
     ComboBoxSearch.Items.Insert(0,ComboBoxSearch.Text);
   end;
end;
  • Save the History of your combobox,for example in the OnClose event of your
    form
procedure TForm1.FormClose(Sender: TObject);
begin
   ComboBoxSearch.Items.SaveToFile(ExtractFilePath(ParamStr(0))+'History.txt');
end;
  • in the Oncreate event of your form you can load the saved items
procedure TForm1.FormCreate(Sender: TObject);
var
 FileHistory  : string;
begin
   FileHistory:=ExtractFilePath(ParamStr(0))+'History.txt';
   if FileExists(FileHIstory) then
   ComboBoxSearch.Items.LoadFromFile(FileHistory);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读