AutoComplete TextBox in C#

Introduction 

 
Most of the developers are familiar with the auto-completion text feature available in browsers, search controls, and other controls. The auto-completion feature is when you start typing some characters in a control, the matching data is loaded automatically for you.

In Visual Studio 2005, some of the controls support this feature including the ComboBox and the TextBox controls. By using these features, we can build Internet Explorer-like auto-completion functionality in our Windows Forms applications.
 
Now, we can have a combo box that completes URLs as soon as you type any character. For example, in Figure 1, I typed "c-s" and I see all the URLs starting with "c-s".
 
AutoCompleteImg1.gif
Figure 1. Auto-Completion in a ComboBox
 
The AutoCompleteSource and AutoCompleteMode properties of the TextBox and ComboBox controls allow developers to provide automatic completion text features. You can set both of these properties at design-time as well as at run-time. If you click on AutoCompleteSource drop down, you will see all the options in the drop-down list. See Figure 2.
 
AutoCompleteImg2.GIF
Figure 2. AutoCompleteSource options
 
Figure 3 shows AutoCompleteMode options.
 
AutoCompleteImg3.GIF
Figure 3. AutoCompleteMode options
 
You can also set these properties at run-time using the following code:  
  1. comboBox1.AutoCompleteSource = AutoCompleteSource.AllSystemSources;  
  2. comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  
The AutoCompleteSource Enumeration has following members:
  • AllSystemResources - Specifies the equivalent of FileSystem and AllUrl as the source. This is the default value when AutoCompleteMode has been set to a value other than the default.
  • AllUrl - Specifies the equivalent of HistoryList and RecentlyUsedList as the source.
  • CustomSource - Specifies strings from a built-in AutoCompleteStringCollection as the source.
  • FileSystem - Specifies the file system as the source. 
  • FileSystemDirectories - Specifies that only directory names and not file names will be automatically completed.
  • HistoryList - Includes the Uniform Resource Locators (URLs) in the history list.
  • ListItems - Specifies that the items of the ComboBox represent the source.
  • None - Specifies that no AutoCompleteSource is currently in use. This is the default value of AutoCompleteSource.
  • RecentlyUsedList - Includes the Uniform Resource Locators (URLs) in the list of those URLs most recently used.
The AutoCompleteMode enumeration has the following members:
  • Append - Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
  • None - Disables the automatic completion feature for the ComboBox and TextBox controls.
  • Suggest - Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
  • SuggestAppend - Applies both Suggest and Append options.
Loading Custom Source 
 
We can also specify a custom source from where the listing will be loaded. If you click on the AutoCompleteCustomSource property, it will open the String Collection Editor, where we can add our strings. For example, I add the following strings to the strings list. See Figure 4.
 
AutoCompleteImg4.GIF
Figure 4. Custom Source strings
 
Now we need to set AutoCompleteSource to CustomSource:
  1. comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;  
And when I run and type "v" in the combo box, I see VB.NET Heaven. See Figure 5.
 
AutoCompleteImg5.GIF
Figure 5. Custom Source listing
 
We can also create AutoCompleteStringCollection programmatically. The following code creates an AutoCompleteStringCollection, adds strings to the collection, and sets it to the AutoCompleteCustomSource of the ComboBox. 
  1. // AutoCompleteStringCollection   
  2. AutoCompleteStringCollection data = new AutoCompleteStringCollection();  
  3. data.Add("Mahesh Chand");  
  4. data.Add("Mac Jocky");  
  5. data.Add("Millan Peter");  
  6. comboBox1.AutoCompleteCustomSource = data;  
Now running the sample and typing "m" in the ComboBox loads the data as shown in Figure 6.
 
AutoCompleteImg6.GIF 
Figure 6. Loading custom data
 

Summary

 
The AutoComplete feature of ComboBox and TextBox controls allow us to set the autocomplete text feature. In this article, we saw how to use this feature in our application at design-time as well as at run-time.