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 Monro
And as I keep typing Mi
then "ke Myers" would appear at the end:
ke Myers
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 tocsDropDown
- 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;