dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: autocomplete combobox problems

  1. #1
    Join Date
    Mar 2002
    Posts
    138

    autocomplete combobox problems

    I have an autocomplete combobox usercontrol on my form which is used to display all the information about a particular inventory item... this is my problem.. I am using an adodc data control to retrieve the data on the particular inventory item.. so after the user chooses the item from the combobox I set the adodc to retrieve the information on that item... well, the only event I know to do this in is the click event of the combobox.. but with the autocomplete combobox they don't necessarily have to click the item to get it.. for instance they type "Ap" and apple comes up in the text box of the combo box.. is there anyway to get around this?

  2. #2
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    is there an Onchange event ? I would work with that maybe.
    Nicolas Bohemier

  3. #3
    Join Date
    Mar 2002
    Posts
    138

    no onchange

    there is a change event but no onchange event...

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    Try to get the value from the combo box on the change event and check if it suits your needs.
    Nicolas Bohemier

  5. #5
    Join Date
    Mar 2002
    Posts
    138

    won't work

    I don't think I can use the change event.. here is a copy of the usercontrol I am using...
    Attached Files Attached Files

  6. #6
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    Firewall don't let me download any files not having pdf extension. So don't bother sending me stuff
    Nicolas Bohemier

  7. #7
    Join Date
    Mar 2002
    Posts
    138

    ok

    Ok, well the change event is what triggers the search for the autocomplete.. unfortunately, I don't think there is a way for me to tell if there was a match found or not.

  8. #8
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    When do you get the item for your Database ? if you get it once user click on something else you could trigger the lostfocus event.

    Or even better, on the validate event.
    Nicolas Bohemier

  9. #9
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    In the Change event you could do something like that.

    If Combo1.Text = Combo1.List(Combo1.ItemData(0)) Then
    MsgBox "test"
    End If

    If your items are sorted in the list and the user is limited to entries in the list you could use a Dichotomic search on the list and once you find it, you trigger the database code. Example

    Code:
    Option Explicit
    Private Sub Combo1_Change()
        If FindItemInList(Combo1.Text, Combo1) Then
            ' Put code to manage the database here
            MsgBox "Found"
        End If
    End Sub
    
    Private Sub Form_Load()
        Combo1.AddItem ("test1")
        Combo1.AddItem ("test2")
        Combo1.AddItem ("test3")
        Combo1.AddItem ("test4")
        Combo1.AddItem ("test5")
        Combo1.Text = Combo1.list(Combo1.ItemData(0))
    End Sub
    
    Private Function FindItemInList(stringtosearch As String, list As ComboBox) As Boolean
        Dim numberofitem As Integer
        Dim Found As Boolean
        Dim currentIndice As Integer
        Found = False
        numberofitem = list.ListCount - 1
        currentIndice = numberofitem \ 2
        Do While Not Found And currentIndice < numberofitem
            If list.list(currentIndice) = stringtosearch Then
                Found = True
            Else
                If stringtosearch > list.list(currentIndice) Then
                    currentIndice = currentIndice + currentIndice \ 2
                ElseIf stringtosearch < list.list(currentIndice) Then
                    currentIndice = currentIndice \ 2
                End If
                If currentIndice = 0 Or currentIndice = numberofitem Then
                    If list.list(currentIndice) = stringtosearch Then
                        Found = True
                    End If
                    Exit Do
                End If
            End If
        Loop
        FindItemInList = Found
    End Function
    Dichotomic search will find an item or not find an item in log (2) of the number of item in the list.

    so if you have 32 items it will take 5 iterations. If you have 65536 items, it will take a mere 16 iterations. so the bigger the list is, the more efficient it becomes.

    That's an average it could take + 1 or 2 iterations.
    Last edited by Boumxyz2; June 26th, 2002 at 10:04 AM.
    Nicolas Bohemier

  10. #10
    Join Date
    Mar 2002
    Posts
    138
    arrrrgggghhhhhh.. why does such a simple thing have to be so **** difficult?

  11. #11
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    I'm trying to help you out. There might be a simpler way.
    Nicolas Bohemier

  12. #12
    Join Date
    Mar 2002
    Posts
    138
    I'm actually playing around with another autocomplete combobox I found.. still don't have it working.

  13. #13
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    I got a simple solution for you but I'm not sure you might want it.
    Just add a button. Update the fields once the user click on the button.
    Nicolas Bohemier

  14. #14
    Join Date
    Mar 2002
    Posts
    138
    that's kind of what I am going to do now.. but I am searching the net for a cool hover button or something

  15. #15
    Join Date
    May 2002
    Posts
    40
    Try this code

    --------------------------

    Option Explicit

    Dim InChangeMode As Boolean
    Dim BackSpacePressed As Boolean

    Private Sub Combo1_Change()
    On Error Resume Next
    Dim idx As Integer
    Dim SelFrom As Integer

    If InChangeMode Then Exit Sub
    If BackSpacePressed Then
    BackSpacePressed = False
    Exit Sub
    End If

    With Combo1
    If Trim(.Text) = "" Then Exit Sub
    If .ListCount < 0 Then Exit Sub
    InChangeMode = True
    For idx = 0 To .ListCount
    If UCase(.Text) = UCase(Left(.List(idx), Len(.Text))) Then Exit For

    '' If your data in Combox is sorted than add the next line
    ' If UCase(.Text) < UCase(Left(.List(idx), Len(.Text))) Then GoTo EndOfProcess

    Next idx
    If idx < .ListCount Then
    SelFrom = Len(.Text)
    .ListIndex = idx
    .SelStart = SelFrom
    .SelLength = Len(.Text) - SelFrom
    End If
    End With
    EndOfProcess:
    InChangeMode = False
    End Sub

    Private Sub Combo1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case vbKeyBack
    BackSpacePressed = True
    End Select

    End Sub

    Private Sub Form_Load()
    Dim idx

    With Combo1
    .Clear
    For idx = 65 To 88
    .AddItem Chr(idx) & Chr(idx + 1) & Chr(idx + 2) & "test"
    Next idx
    End With
    InChangeMode = False
    BackSpacePressed = False
    End Sub

    ------------------------------------

    efi

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)