Problem
You want to display a combo box or text box that automatically completes what the user is typing based on a list of predefined items.
Solution
Configure the autocomplete features of the standard .NET ComboBox or TextBox control. The
AutoCompleteMode property controls the autocompletion behavior, and the AutoCompleteSource property allows you to specify the source of the autocomplete data.
How It Works
Autocomplete functionality is common and comes in many different variations. For example, a control values), or the control might display a drop-down list of near matches (as Microsoft Internet Explorer does when you are typing a URL). The AutoCompleteMode takes one of the following values, which define how the control’s autocomplete behavior works:
• None: Autocomplete is disabled. This is the default behavior for ComboBox and TextBox.
• Suggest: This displays suggestions as a drop-down list.
• Append: This appends the remainder of the most likely suggestion to the end of the
text as the user enters it.
• SuggestAppend: This combines the functionality of both Suggest and Append.
The AutoCompleteSource property defines where the ComboBox or TextBox control sources the
autocomplete suggestions it presents to the user. It is possible to make use of various system-level data sources like the file system or URL histories. The most commonly used values for the
AutoCompleteSource property are ListItems, where the ComboBox uses its current content and
CustomSource. If you specify CustomSource, you must populate the AutoCompleteCustomSource property of the ComboBox or TextBox with the set of strings you want to use as autocomplete suggestions.
The Code
The following example enables autocomplete on a ComboBox and populates it with a list of values using a custom source.
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace Apress.VisualCSharpRecipes.Chapter07
{
public partial class Recipe07_09 : Form
{
public Recipe07_09()
{
// Initialization code is designer generated and contained
// in a separate file named Recipe07-09.Designer.cs.
InitializeComponent();
// Configure ComboBox1 to make its autocomplete
// suggestions from a custom source.
this.comboBox1.AutoCompleteCustomSource.AddRange(
new string[] { "Man", "Mark", "Money", "Motley",
"Mostly", "Mint", "Minion", "Milk", "Mist",
"Mush", "More", "Map", "Moon", "Monkey"});
this.comboBox1.AutoCompleteMode
= AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource
= AutoCompleteSource.CustomSource;
// Configure ComboBox2 to make its autocomplete
// suggestions from its current contents.
this.comboBox2.Items.AddRange(
new string[] { "Man", "Mark", "Money", "Motley",
"Mostly", "Mint", "Minion", "Milk", "Mist",
"Mush", "More", "Map", "Moon", "Monkey"});
this.comboBox2.AutoCompleteMode
= AutoCompleteMode.SuggestAppend;
this.comboBox2.AutoCompleteSource
= AutoCompleteSource.ListItems;
// Configure ComboBox3 to make its autocomplete
// suggestions from the system's URL history.
this.comboBox3.AutoCompleteMode
= AutoCompleteMode.SuggestAppend;
this.comboBox3.AutoCompleteSource
= AutoCompleteSource.AllUrl;
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Recipe07_09());
}
}
}
If you have any question the please don't hesitate in asking :)