WinForms New AutoComplete Functionality

Posted by on October 23, 2007

One of the nice new features that have been added to winforms in 2.0 is the AutoComplete functionality for textboxes and comboxes. By setting up a data source (really just a source of strings) one can quickly add auto complete functionality to their forms. It is pretty simple to set up the auto complete functionality. There are three properties that are associated with the auto complete:

  • AutoCompleteMode – Specifies the mode that the automatic completion will use.
    • Append – Appends the remainder of the first candidate string to the existing text, highlighting the remaining characters.
    • None – Disables automatic completion feature.
    • Suggest – Displays a dropdown list of suggested completion strings.
    • SuggestAppend – Applies both Suggest and Append options.
  • AutoCompleteSource – Specifies the source of strings to make suggestions from.
    • AllSystemSources – This is the equivalent of FileSystem and AllUrl as the source. This is the default if AutoCompleteMode is set to a value other than “None”.
    • AllUrl – This is the equivalent of HistoryList and RecentlyUsedList as the source.
    • CustomSource – This specifies strings stored in an AutoCompleteStringCollection. This requires the AutoCompleteCustomSource property to be set.
    • FileSystem – Specifies the file system as the source.
    • FileSystemDirectories – Specifies that only Directory names will be completed.
    • HistoryList – Includes the urls in the history list.
    • ListItems – (combobox) specifies that the items in the combobox act as the source.
    • None – Specifies that no source is in use. This is the default.
    • RecentlyUsedList – Includes URLs in the list of those most recently used.
  • AutoCompleteCustomSource – Specifies a custom AutoCompleteStringCollection as the source for the suggestions.

What is Supported

The auto complete functionality is only supported by the textbox and combobox control. To further limit this list, the textbox only supports this when in single line mode. Multi-line mode ignores any settings for the AutoComplete properties.

The AutoCompleteMode is pretty self explanatory. You might, however, be confused on some of the AutoCompleteSource items. The difference between FileSystem and FileSystemDirectories is that FileSystem will also list files in the suggestion and FileSystemDirectories will only list folders. Both, however, appear to not work at first glance. If you type in C: you will not see any suggestions. This may make you think it is not working but as soon as you type C:\ you will see the list of suggestions. As you continue building onto the path by adding more ‘\’ characters you will continue to receive more suggestions. The url types work a little better because as soon as you type the ‘h’ in http you will start to see a list of suggestions.

So far we have seen how easy it is to set up the textbox or combobox to auto suggest data based on built in values. Although, actually seeing this work makes one wonder where they pull that information from. The real question is how do I add my own list of suggestions. This is where the AutoCompleteCustomSource comes into play. The designers have graciously given us a new Collection called AutoCompleteStringCollection. All this collection does is store a list of string values. Once you populate the collection with your list of suggestions then you can set the AutoCompleteCustomSource to your new collection.

Example CustomCollection

Dim _myCollection as New AutoCompleteStringCollection() 
For _index as Int32 = 0 to 10 
_myCollection.Add(String.Format(“Custom String {0}.”,_index)) 
Next 
txtSuggestions.AutoCompleteCustomSource = _myCollection 
txtSuggestions.AutoCompeteMode = AutoCompleteMode.Suggest 
txtSuggestions.AutoCompleteSource = AutoCompleteSource.CustomSource 

When the above code is added and the form is run then as the user types in the values Custom String the suggestions will appear in the dropdown that is displayed.

(this was originally posted on my original blog on 7/27/2006)

Comments

Comments are closed.