Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Oct 8th, 2003 05:38 AM Post subject: AutoComplete Combo Box |
|
|
Below is the code for AutoComplete Combo Box just like Internet Explorer's Address Bar. We have used combo box named cboCode in the following example!
| Code: | Private Sub cboCode_KeyDown(keycode As Integer, Shift As Integer)
If keycode = vbKeyDelete Then
cboCode.Text = ""
keycode = 0
End If
End Sub
Private Sub cboCode_KeyPress(KeyAscii As Integer)
Dim strSearchTextAs String
Dim strEnteredText As String
Dim intLengthAs Integer
Dim intIndexAs Integer
Dim intCounter As Integer
On Error Goto ErrorHandler
With cboCode
If .SelStart > 0 Then
strEnteredText = Left(.Text, .SelStart)
End If
Select Case KeyAscii
Case vbKeyReturn
If .ListIndex > -1 Then
.SelStart = 0
.SelLength = Len(.List(.ListIndex))
Exit Sub
End If
Case vbKeyEscape, vbKeyDelete
.Text = ""
KeyAscii = 0
Exit Sub
Case vbKeyBack
If Len(strEnteredText) > 1 Then
strSearchText = LCase(Left(strEnteredText, Len(strEnteredText) - 1))
Else
strEnteredText = ""
KeyAscii = 0
.Text = ""
Exit Sub
End If
Case Else
strSearchText = LCase(strEnteredText & Chr(KeyAscii))
End Select
intIndex = -1
intLength = Len(strSearchText)
For intCounter = 0 To .ListCount - 1
If LCase(Left(.List(intCounter), intLength)) = strSearchText Then
intIndex = intCounter
Exit For
End If
Next intCounter
If intIndex > -1 Then
.ListIndex = intIndex
.SelStart = Len(strSearchText)
.SelLength = Len(.List(intIndex)) - Len(strSearchText)
Else
Beep
End If
End With
KeyAscii = 0
Exit Sub
ErrorHandler:
KeyAscii = 0
Beep
End Sub
Private Sub cboCode_LostFocus()
cboCode.SelLength = 0
End Sub |
|
|