There are times when you’ll need an autocomplete textbox. Autocomplete textbox makes the user’s life easier. It shows suggestions as the user types. In case of a web browser, if you type “goo” it will automatically show all the suggestions starting with or containing “goo” like “Google, Goofy” etc. It really helps both you or your users when hunting for something. Of course, it seems difficult to set up, but the reality is very different. So whether they’re typing in “find O2 UK BROADBAND” or you’re hunting for “that holiday video”, then your users are likely to find it a lot faster. Enough talk! Lets get on with it.
Fortunately, textbox has a built-in autocomplete feature! We don’t have to do any coding.
You can either set it up from the Designer (if you are using Visual Studio or #Develop) or do it via code. It is simple both ways. Lets do it!
Setting up AutoComplete
For setting up autocomplete you’ll have to use three properties of the TextBox class — AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode. Here’s an explanation of these properties:
AutoCompleteSource is the source of the suggestions. You can assign it CustomSource if you want to use custom suggestions or use other auto sources like FileSystem, HistoryList etc. Below are all the available sources:
- FileSystem: This will show suggestions from the file system i.e files stored on the computer. Like if you write “C:\” it’ll show all the files stored on C: drive.
- FileSystemDirectories: This is same as FileSystem but it shows directory names instead of file names.
- HistoryList: This will show URLs from Internet Explorer’s history.
- RecentlyUsedList: A list of recently used applications, folders, and URLs (from internet explorer).
- AllUrl: This specifies an equivalent of HistoryList and RecentlyUsedList as the source.
- AllSystemSources: Specifies an equivalent of AllUrl and FileSystem as the source.
AutoCompleteModes are of 3 types. Suggest, Append and SuggestAppend:
- Suggest will show suggestions in a drop-down as the user types.
- Append will auto complete as you type. Like if you write “Goo” it’ll automatically make it “Google”.
- SuggestAppend will do both Suggest/Append.
AutoCompleteCustomSource is collection of custom suggestions. It will be used when you assign “CustomSource” as “AutoCompleteSource”.
Okay enough explanations. Lets make an autocomplete textbox now.
Using Designer:
If you are using the Designer, then all you have to do is to select your TextBox and change those three properties (explained above). For instance, if you want to use custom suggestions then use “CustomSource” and add the custom suggestions to “AutoCompleteCustomSource” (don’t forget to set up AutoCompleteMode). Debug your project and see! Its that easy
. Don’t forget that you can also use all the other sources like FileSystem, HistoryList etc.
Via Coding:
Here’s an example:
// List of custom suggestions
string[] suggestions = new string[] {
"Google",
"Google Images",
"Yahoo",
"Youtube"
};
// Use the AutoCompleteMode that suits you.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
// Since we are using custom suggestions you
// should use this source.
// Use the other non-custom sources if you
// don't want to use custom suggestions.
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
// And finally add the above suggestions to the CustomSource
textBox1.AutoCompleteCustomSource.AddRange(suggestions);
That’s it.
If you have any questions, don’t hesitate to post a comment.



I don’t have the 3 options “AutoCompleteMode”, “AutocompleteSource” and “AutoCompleteCustomSource”. I only got “AutoCompleteType”.
I’m using system.windows.forms.
How do i get the 3 options?
Are you using ASP.NET?
hi
its usefull … i have a questions … if i use it for textbox in tab control it throw a exception whit this message : Error creating window handle
this is my Code:
AutoCompleteStringCollection data = new AutoCompleteStringCollection();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = new myConnection().Cnn;
cmd1.CommandText = “select * from RegCustomer”;
SqlDataReader dr1 = cmd1.ExecuteReader();
ArrayList ListArray = new ArrayList();
while (dr1.Read())
{
ListArray.Add(dr1[2].ToString());
}
for (int i = 0; i < ListArray.Count; i++)
{
data.Add(ListArray[i].ToString());
}
txtCustomerName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtCustomerName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtCustomerName.AutoCompleteCustomSource = data;
That error has nothing to do with the AutoComplete.
Check this link:
http://stackoverflow.com/questions/….
I’m using ASP.net.
How do i get the 3 options (“AutoCompleteMode”, “AutocompleteSource” and “AutoCompleteCustomSource”) ?
Or with other sintax???
Thanx
Sorry, those options might not be available in ASP.NET. Google it.
Here’s one:
http://www.asp.net/…/AutoComplete.aspx
Thanks
Farooq, there isn’t any such properties either in visual studio or web devloper.Images he has palced over here are morphed to make people fool.
@kjh: Sorry for the confusion. You can check all the three properties at MSDN website:
- AutoCompleteSource
- AutoCompleteCustomSource
- AutoCompleteMode
You can check that yourself. By the way, are you using ASP.NET?
How to download it?
Hiiiiiiiii Sir.This is abhinay from Vizag.
How to implement AutoCompleteExtender for single textbox for multiple attributes to retrieve the values?
For example I am having a student database with attributes RollNo,Name,Address,Marks.
Now i want to implement AutoCompleteExtender for single textbox for the attributes i.e.RollNo,Name,Address,Marks.to retrieve the values?Is it possible.If so please suggest me how to approach. And also please explain me with an example.
Thank you in Advance
Hi Kumar,
I am sorry AutoCompleteExtender is an ASP.NET control and I am not good with ASP.NET.
Check this link:
http://www.codeproject.com/KB/….
Hope that helps.
Great……
I thinking about do it my self………
You save my time.