Sunday, July 16, 2006

Using the AutoComplete feature in .net 2.0 with a custom source

The .net 2.0 Windows forms TextBox control introduces a new auto complete feature, like that in IE when we try to type a recently visited URL


This feature allow for auto complete of strings from a certain standard sources like File System and IE History List, but it also allow for a custom source to be used as the source of strings to be auto completed



The custom source can be a list of strings added in runtime by setting the AutoCompleteCustomeSource property, or at runtime by setting this property to an AutoCompleteStringCollection instance.

We should set the AutoCompleteSource to CustomSource first and then set the AutoCompleteCustomeSource property.




private void Form1_Load(object sender, EventArgs e)
{

AutoCompleteStringCollection names = new AutoCompleteStringCollection();
names.Add("Apple");
names.Add("Bird");
names.Add("Car");
names.Add("Dog");

textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = names;


}

No comments: