Make a TextBox with auto-complete in C#


It’s not too hard to make a TextBox provide an auto-complete feature. In fact, the TextBox control offers three auto-complete styles. This example demonstrates those styles.

To provide auto-complete, you need to set the following three TextBox properties.

  • AutoCompleteMode – This indicates the style that TextBox uses. The options are:
    • None – The user types everything.
    • Append – The TextBox fills in a suggestion. The user can press Tab to accept the suggestion.
    • Suggest – The TextBox displays a dropdown holding possible matches. The user can arrow down to a choice and press Tab to accept it.
    • SuggestAppend – The TextBox both fills in a suggestion and displays a dropdown.
  • AutoCompleteSource – This indicates where the TextBox gets its auto-complete values. The options are:
    • None – The control doesn’t provide auto-complete.
    • FileSystem – Uses the file system.
    • FileSystemDirectories – Uses directories in the file system.
    • HistoryList – Uses URIs in the history list.
    • RecentlyUsedList – Uses most recently used URIs in the history list.
    • AllUrl – HistoryList plus RecentlyUsedList.
    • AllSystemSources – FileSystem plus AllUrl.
    • CustomSource – An AutoCompleteStringCollection provided by the code. This is the value demonstrated by this example.
  • AutoCompleteCustomSource The AutoCompleteStringCollection to use when AutoCompleteSource is CustomSource.

When the program starts, it uses the following code to prepare the program’s TextBox controls.

private void Form1_Load(object sender, EventArgs e)
    // Make the collection of day names.
    AutoCompleteStringCollection day_source =
        new AutoCompleteStringCollection();

    // Prepare the TextBox.
    PrepareTextBox(txtAppend, day_source, AutoCompleteMode.Append);
    PrepareTextBox(txtSuggest, day_source, AutoCompleteMode.Suggest);
    PrepareTextBox(txtSuggestAppend, day_source,

This code creates a new AutoCompleteStringCollection object. It then uses Enum.GetNames to get the names of the days of the week from the DayOfWeek enumeration and adds them to the source.

The code then calls the following PrepareTextBox method for three of the program’s TextBox controls.

// Prepare a TextBox for auto-completion.
private void PrepareTextBox(TextBox txt, 
    AutoCompleteStringCollection source, AutoCompleteMode mode)
    txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txt.AutoCompleteCustomSource = source;
    txt.AutoCompleteMode = mode;

This method simply sets the TextBox control’s auto-complete properties.

Download the example and experiment with it to see which auto-complete style you prefer.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in controls, user interface and tagged , , , , , , , , , . Bookmark the permalink.