Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base

Post new topic   Reply to topic
AutoComplete Combo Box
View previous topic :: View next topic  
Author Message
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Oct 8th, 2003 05:38 AM    Post subject: AutoComplete Combo Box Reply with quote

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
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic