[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Make a TextBox with autocomplete in C#

[Make a TextBox with autocomplete 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 the 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(); day_source.AddRange(Enum.GetNames(typeof(DayOfWeek))); // Prepare the TextBox. PrepareTextBox(txtAppend, day_source, AutoCompleteMode.Append); PrepareTextBox(txtSuggest, day_source, AutoCompleteMode.Suggest); PrepareTextBox(txtSuggestAppend, day_source, AutoCompleteMode.SuggestAppend); }

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 to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.