This step-by-step article describes how to implement an automatic text-completion feature in a
ComboBox control by using Visual C#.
Description
You can enter data in a ComboBox control either by typing a value or by clicking a value in the list. When you type a value, it is faster to type only the first few characters of the value and then have the
ComboBox control display the closest match from the list of values automatically. Many Microsoft products use this feature. For example, Microsoft Money uses this feature to select the
Payee value when you write a check. Microsoft Internet Explorer uses this feature when you type a Web address. Visual Studio .NET uses this feature for IntelliSense. You can see this feature in use if you type the following line of code:
Typing this line automatically displays "System.Console." This automatic text-completion feature can save time and help to prevent data-entry errors. This article demonstrates how to implement this functionality in your Visual C# .NET application.
How to Use the Sample Code
- Create a new Windows Application project in Visual C#. Form1 is created automatically.
- Add a ComboBox control to Form1.
- Add the following code to the Load event of the form:
// Add some items to the ComboBox list.
this.comboBox1.Text = "";
this.comboBox1.Items.Add("a");
this.comboBox1.Items.Add("aaa");
this.comboBox1.Items.Add("combo");
this.comboBox1.Items.Add("combobox");
this.comboBox1.Items.Add("combobox test");
this.comboBox1.Items.Add("common");
this.comboBox1.Items.Add("common dialog");
- Add the following code to the KeyUp event of the ComboBox control:
int index;
string actual;
string found;
// Do nothing for certain keys, such as navigation keys.
if ((e.KeyCode == Keys.Back) ||
(e.KeyCode == Keys.Left) ||
(e.KeyCode == Keys.Right) ||
(e.KeyCode == Keys.Up) ||
(e.KeyCode == Keys.Down) ||
(e.KeyCode == Keys.Delete) ||
(e.KeyCode == Keys.PageUp) ||
(e.KeyCode == Keys.PageDown) ||
(e.KeyCode == Keys.Home) ||
(e.KeyCode == Keys.End))
{
return;
}
// Store the actual text that has been typed.
actual = this.comboBox1.Text;
// Find the first match for the typed value.
index = this.comboBox1.FindString(actual);
// Get the text of the first match.
if (index > -1)
{
found = this.comboBox1.Items[index].ToString();
// Select this item from the list.
this.comboBox1.SelectedIndex = index;
// Select the portion of the text that was automatically
// added so that additional typing replaces it.
this.comboBox1.SelectionStart = actual.Length;
this.comboBox1.SelectionLength = found.Length;
}
Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The Designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls.
For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site: - Save and then run the sample.
- Type some values. As you type the text, a value is automatically selected if the list of values contains an exact match. If a value in the list begins with the characters that you type, that value is displayed. The portion of the value that you typed is highlighted so that additional typing replaces it. The following are some of the results from using this sample code:
Collapse this tableExpand this table
| Type | Resulting String |
|---|
| a | a |
| aa | aaa |
| com | combo |
| comm | common |
| combob | combobox |
| combobox<SPACEBAR> | combobox test |