Use autocomplete features in C#

Some controls, such as the ComboBox and TextBox, support auto-completion. They provide three properties to let you determine how they support this:

  • AutoCompleteMode - This determines how (if at all) the control supports auto-completion.
    • None - The control doesn't support auto-completion.
    • Suggest - The control displays a drop-down list with suggestions. (This is what Windows Explorer does if you type in its top address area.)
    • Append - The control displays possible matches inside its text area with text after what you have typed highlighted.
    • SuggestAppend - Both Suggest and Append.
  • AutoCompleteSource - This tells the control what values to use for auto-completion.
    • FileSystem - Use the file system. (This is what Windows Explorer does if you type in its top address area.)
    • HistoryList - Use browser URLs.
    • RecentlyUsedList - Use recently used browser URLs.
    • AllUrl - HistoryList plus RecentlyUsedList.
    • AllSystemSources - FileSystem plus AllUrl.
    • FileSystemDirectories - Use the file system but only select directories not files.
    • CustomSource - A custom source you create.
    • None - No auto-completion.
  • AutoCompleteCustomSource - If you set AutoCompleteSource to CustomSource, then you can type a list of values to use here.

At design time, I set properties on the first two groups of TextBoxes. In the first group, I set AutoCompleteSource = FileSystem. In the second group, I set AutoCompleteSource = CustomSource. I also set the AutoCompleteMode properties to Suggest or Append for the various controls.

The program uses the following code to set the auto-completion properties for the last two TextBoxes at run time.

// Prepare the last group of auto-completion TextBoxes.
private void Form1_Load(object sender, EventArgs e)
{
    // Make an auto-completion source.
    AutoCompleteStringCollection source =
        new AutoCompleteStringCollection();
    source.Add("Agriculture");
    source.Add("Commerce");
    ...
    source.Add("Veterans Affairs");

    txtCodeSuggest.AutoCompleteMode = AutoCompleteMode.Suggest;
    txtCodeSuggest.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txtCodeSuggest.AutoCompleteCustomSource = source;

    txtCodeAppend.AutoCompleteMode = AutoCompleteMode.Append;
    txtCodeAppend.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txtCodeAppend.AutoCompleteCustomSource = source;
}

The code creates a AutoCompleteStringCollection object to hold strings for use with auto-completion. It then adds a bunch of string values to the collection.

The code then sets the two TextBoxes' auto-completion properties appropriately. In particular, it sets their AutoCompleteCustomSource properties to the collection of values.

Auto-completion certainly isn't appropriate for every TextBox, but it can make entering some values easier. For example, in some circumstances it might be easier for the user to select a file this way instead of using an OpenFileDialog. (Or you could put a button with an ellipsis to the right to give the use both options.)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.