Microsoft KB Archive/320107

= How to provide automatic text completion for a ComboBox control in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 320107

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q320107



For a Microsoft C# .NET version of this article, see 319946.

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 Basic .NET or Visual Basic 2005.

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 have 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 or Visual Studio 2005 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 Basic .NET or Visual Basic 2005 application.

back to the top

How to Use the Sample Code
 Create a new Windows Application project in Visual Basic .NET or Visual Basic 2005. Form1 is created automatically.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

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

 Add a ComboBox control to Form1.  Add the following code to the Load event of the form: ' Add some items to the ComboBox list. Me.ComboBox1.Text = &quot;&quot; Me.ComboBox1.Items.Add(&quot;a&quot;) Me.ComboBox1.Items.Add(&quot;aaa&quot;) Me.ComboBox1.Items.Add(&quot;combo&quot;) Me.ComboBox1.Items.Add(&quot;combobox&quot;) Me.ComboBox1.Items.Add(&quot;combobox test&quot;) Me.ComboBox1.Items.Add(&quot;common&quot;) Me.ComboBox1.Items.Add(&quot;common dialog&quot;)   Add the following code to the KeyUp event of the ComboBox control: Dim index As Integer Dim actual As String Dim found As String

' Do nothing for some keys such as navigation keys. If ((e.KeyCode = Keys.Back) Or _   (e.KeyCode = Keys.Left) Or _    (e.KeyCode = Keys.Right) Or _    (e.KeyCode = Keys.Up) Or _    (e.KeyCode = Keys.Delete) Or _    (e.KeyCode = Keys.Down) Or _    (e.KeyCode = Keys.PageUp) Or _    (e.KeyCode = Keys.PageDown) Or _    (e.KeyCode = Keys.Home) Or _    (e.KeyCode = Keys.End)) Then

Return End If

' Store the actual text that has been typed. actual = Me.ComboBox1.Text

' Find the first match for the typed value. index = Me.ComboBox1.FindString(actual)

' Get the text of the first match. If (index > -1) Then found = Me.ComboBox1.Items(index).ToString

' Select this item from the list. Me.ComboBox1.SelectedIndex = index

' Select the portion of the text that was automatically ' added so that additional typing will replace it. Me.ComboBox1.SelectionStart = actual.Length Me.ComboBox1.SelectionLength = found.Length End If                    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: kbvs2005swept kbvs2005applies kbhowtomaster KB320107

-

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

© Microsoft Corporation. All rights reserved.