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:
- 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:
- 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 = ""
- 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
' 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
- 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:
Characters that you type Resulting string a a aa aaa com combo comm common combob combobox combobox<SPACEBAR> combobox test
ID do Artigo: 320107 - Última Revisão: 11 de mai de 2007 - Revisão: 1