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

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

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.

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


How to Use the Sample Code

  1. 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:

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

  2. Add a ComboBox control to Form1.
  3. Add the following code to the Load event of the form:
    ' Add some items to the ComboBox list.
    Me.ComboBox1.Text = ""
    Me.ComboBox1.Items.Add("a")
    Me.ComboBox1.Items.Add("aaa")
    Me.ComboBox1.Items.Add("combo")
    Me.ComboBox1.Items.Add("combobox")
    Me.ComboBox1.Items.Add("combobox test")
    Me.ComboBox1.Items.Add("common")
    Me.ComboBox1.Items.Add("common dialog")
  4. 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
  5. Save and then run the sample.
  6. 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:
    Characters that you typeResulting string
    aa
    aaaaa
    comcombo
    commcommon
    combobcombobox
    combobox<SPACEBAR>combobox test
Propriedades

ID do Artigo: 320107 - Última Revisão: 11 de mai de 2007 - Revisão: 1

Comentários