C# – Autocomplete Textbox
Posted on May 15th, 2010 in C#, Tutorials | 6 Comments »
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. 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.



6 Responses
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:
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/ajax/ajaxcontroltoolkit/Samples/AutoComplete/AutoComplete.aspx
Thanks