从文本文件自动追加/完成到编辑框 delphi [英] Auto append/complete from text file to an edit box delphi

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

问题描述

我正在尝试创建一个编辑框,我希望它能够在键入时自动附加输入的文本.文本将附加来自文本文件的建议".

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

当我开始在编辑框中输入M"时,其余的将突出显示(或不突出显示):艾琳梦露"当我继续输入Mi"时,最后会出现ke Myers".我希望我对你们说得够清楚了!感谢您的帮助!

解决方案

您可以使用 <代码>TComboBox.

请按照以下步骤操作:

<块引用>
  • 在表单中放置一个组合框
  • autocomplete 属性设置为 true
  • sorted 属性设置为 true
  • style 属性设置为 csDropDown
  • 在组合框的OnExit事件中添加这样的代码

constMaxHistory=200;//最大项目数过程 TForm1.ComboBoxSearchExit(Sender: TObject);开始//检查输入的文本是否存在于列表中,如果不存在则添加到列表中如果 (Trim(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 事件中表单

procedure TForm1.FormClose(Sender: TObject);开始ComboBoxSearch.Items.SaveToFile(ExtractFilePath(ParamStr(0))+'History.txt');结尾;

<块引用>

  • 在表单的 Oncreate 事件中,您可以加载保存的项目

procedure TForm1.FormCreate(Sender: TObject);无功文件历史:字符串;开始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;

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

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