Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-Populate Combo Boxes 2

Status
Not open for further replies.

jcg6

Technical User
Feb 18, 2002
50
US
Does anyone know an easy way to auto-populate combo boxes as you type? For example if you want to select the name 'Smith' from a drop-down list - if you type Smi - it will take you to that value automatically. This is easily done in Access, but I'm not sure how to do it in VB.NET. Thanks in advance for any assistance.
 
I thought the standard vb.net combo box already did this??
 
Doesn't seem to work for me. Does anyone have a step-by-step example of creating a combo box? I am fairly new to VB.Net and want to make sure that I am not missing a step. Thanks.
 
I agree with jcg6. The vb combo box does not auto select the closest match as a user types. Instead it will select only the 1st match of the last character a user types. If there is a property to control this, I am not aware of it.
 
Try this.

Public Sub AutoSelect(ByVal cbo As ComboBox, _
ByVal e As System.Windows.Forms.KeyEventArgs)
'This works for comboboxes where the
'DropDownStyle = DropDown

'Place AutoSelect sub in a global module

'Add strFind to global module also, like this...
'Public strFind As String

'Use AutoSelect like this, with the KeyUp event...
'Private Sub myComboBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myComboBox.KeyUp
'AutoSelect(myComboBox, e)
'End Sub

'When focus leaves the combobox
'reset strFind to "" like this...
'Private Sub myComboBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.LostFocus
' strFind = ""
'End Sub

'This is where the code for AutoSelect starts...

Dim intIndex As Integer
Dim strFound As String

'nothing to do here
'exit sub for navigation keys
If ((e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) 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
Exit Sub
End If

'if the combobox receives the focus
'via tab or enter, do nothing
If e.KeyData.ToString = "Tab" Or strFind = "Enter" Then
Exit Sub
End If

'the user entered a space
'don't attach e.KeyData to strFind
If e.KeyData.ToString = "Space" Then
strFind = strFind & " "
GoTo jumptohere
End If


'the user entered a backspace
'set strFind to the same length as
'the current text in the combo box
'don't attach e.KeyData to strFind
If e.KeyData.ToString = "Back" And Len(strFind) > 0 Then
cbo.SelectionStart = cbo.Text.Length
strFind = Mid(cbo.Text, 1, Len(cbo.Text))
Exit Sub
ElseIf e.KeyData.ToString = "Back" Then
'there is no more text in the combobox
'and the user kept hitting backspace
Exit Sub
End If

'attach e.KeyData to strFind
strFind = strFind & e.KeyData.ToString

jumptohere:

'Get the index of the first match
intIndex = cbo.FindString(strFind)

'If there is a match get the text of the first match.
If (intIndex > -1) Then

strFound = cbo.Items(intIndex).ToString()

' Select this item from the list.
cbo.SelectedIndex = intIndex

' Select the portion of the text that was automatically
' added so that additional typing will replace it.
cbo.SelectionStart = strFind.Length
cbo.SelectionLength = strFound.Length

'else, there was no match
Else

'if there isn't a match and the text typed in is only 1 character
'or nothing then just select the first entry in the combo box.
If strFind.Length = 1 Or strFind.Length = 0 Then

cbo.SelectedIndex = 0
cbo.SelectionStart = 0
cbo.SelectionLength = Len(cbo.Text)

Else

'limit the user to choices only
'available in the combobox
cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
cbo.SelectionStart = cbo.Text.Length
strFind = Mid(cbo.Text, 1, Len(cbo.Text))

End If

End If

End Sub
 
i found this on the ms site and works ok for me


'' Track if a special key is pressed
' (in which case the text replacement operation will be skipped).
Private ControlKey As Boolean = False

' Determine whether a special key was pressed.
Private Sub Combobox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles ComboBox1.KeyPress
' Retrieve a reference to the ComboBox that sent this event.
Dim Combo As ComboBox = CType(sender, ComboBox)

If Asc(e.KeyChar) = Keys.Escape Then
' Clear the text.
Combo.SelectedIndex = -1
Combo.Text = ""
ControlKey = True
ElseIf Char.IsControl(e.KeyChar) Then
ControlKey = True
Else
ControlKey = False
End If
End Sub

' Perform the text substituion.
Private Sub Combobox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
' Retrieve a reference to the ComboBox that sent this event.
Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" And Not ControlKey Then
' Search for a matching entry.
Dim MatchText As String = Combo.Text
Dim Match As Integer = Combo.FindString(MatchText)

' If a matching entry is found, insert it now.
If Match <> -1 Then
Combo.SelectedIndex = Match

' Select the added text so it can be replaced
' if the user keeps typing.
Combo.SelectionStart = MatchText.Length
Combo.SelectionLength = Combo.Text.Length - Combo.SelectionStart
End If

End If
End Sub
 
Thanks TennesseeFox! [wiggle] [wiggle] [wiggle]

I have beating my head against my desk for a day and half over this ( [banghead] ).

Now I can move on to something else to lose my (alleged) mind over! [bugeyed] [idea] [cry]

[spidey] [americanflag] [unclesam]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top