Microsoft KB Archive/319946

= How to provide automatic text completion for a ComboBox control in Visual C# =

Article ID: 319946

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q319946



For a Microsoft Visual Basic .NET version of this article, see 320107.

IN THIS TASK
SUMMARY
 * Description
 * How to Use the Sample Code



SUMMARY
This step-by-step article describes how to implement an automatic text-completion feature in a ComboBox control by using Visual C#.

back to the top

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: System.cons Typing this line automatically displays &quot;System.Console.&quot; 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.

back to the top

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 = &quot;&quot;; this.comboBox1.Items.Add(&quot;a&quot;); this.comboBox1.Items.Add(&quot;aaa&quot;); this.comboBox1.Items.Add(&quot;combo&quot;); this.comboBox1.Items.Add(&quot;combobox&quot;); this.comboBox1.Items.Add(&quot;combobox test&quot;); this.comboBox1.Items.Add(&quot;common&quot;); this.comboBox1.Items.Add(&quot;common dialog&quot;);   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:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx

 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: </ol>

back to the top

Additional query words: AutoComplete Auto Complete Fill AutoSelect AutoExpand Expand Pick

Keywords: kbcombobox kbctrl kbhowtomaster KB319946

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.