自动附加/从文本文件的完整,以一个编辑框德尔福 [英] Auto append/complete from text file to an edit box delphi

查看:86
本文介绍了自动附加/从文本文件的完整,以一个编辑框德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个编辑框,我希望它能够自动追加,而打字输入的文本。文本将与从一个文本文件建议被追加。

比方说,我在我的建议文件中有这些:
玛丽莲·梦露
马龙·白兰度
麦克·梅尔斯

当我开始在编辑框中键入M,剩下的将突出显示(或没有):
arilyn梦露
当我继续键入米,然后柯迈尔斯将出现在年底。我希望我做够你们这清楚!感谢您的帮助!


解决方案

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

请按照下列步骤操作:


  

      
  • 在表单拖放组合框

  •   
  • 自动完成属性设置为true

  •   
  • 整理属性设置为true

  •   
  • 风格属性 csDropDown

  •   
  • 在组合框的 OnExit 事件添加一个code这样

  •   

 常量
MaxHistory = 200; //最大项目数
程序TForm1.ComboBoxSearchExit(发件人:TObject的);
开始
   //检查输入的文本列表中的存在,如果不添加到列表
   如果(修剪(ComboBoxSearch.Text)所述;>'')和(ComboBoxSearch.Items.IndexOf(ComboBoxSearch.Text)= - 1),然后
   开始
     如果ComboBoxSearch.Items.Count = MaxHistory然后
     ComboBoxSearch.Items.Delete(ComboBoxSearch.Items.Count-1);
     ComboBoxSearch.Items.Insert(0,ComboBoxSearch.Text);
   结束;
结束;


  

      
  • 保存您的组合框的历史,例如在OnClose事件的
      形式

  •   

 程序TForm1.FormClose(发件人:TObject的);
开始
   ComboBoxSearch.Items.SaveToFile(ExtractFilePath(ParamStr这(0))+'History.txt');
结束;


  

      
  • 在窗体的OnCreate事件时,可以加载保存的项目

  •   

 程序TForm1.FormCreate(发件人:TObject的);
VAR
 FileHistory:字符串;
开始
   FileHistory:= ExtractFilePath(ParamStr这(0))+'History.txt';
   如果FILEEXISTS(FileHIstory),然后
   ComboBoxSearch.Items.LoadFromFile(FileHistory);
结束;

I'm trying to create an edit box and I want it to be able to auto-append the text entered while typing. Text would be appended with "suggestions" from a text file.

Let's say I have these in my suggestion file: Marilyn Monroe Marlon Brando Mike Myers

As I start typing "M" in the edit box, the remaining would appear highlighted(or not): "arilyn Monroe" And as I keep typing "Mi" then "ke Myers" would appear at the end. I hope I'm making this clear enough for you guys! Thanks for your help!

解决方案

You can implement this feature easily using a TComboBox.

follow these steps :

  • 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;

这篇关于自动附加/从文本文件的完整,以一个编辑框德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆