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

前端之家收集整理的这篇文章主要介绍了自动附加/完成从文本文件到编辑框delphi前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个编辑框,我希望它能够自动附加输入时输入的文本.文本将从文本文件中附加“建议”.

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

当我开始在编辑框中输入“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;
原文链接:https://www.f2er.com/delphi/102811.html

猜你在找的Delphi相关文章