Results 1 to 4 of 4

Thread: Searching listboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50

    Cool

    I have a listbox sorted alphabetically, with a text box above. What I want to do is allow the user to type in a search in the text box and as they type the list box selection changes gradually.

    E.g if my list is
    Arsenal
    Chelsea
    Man City
    Man United

    as the user types 'M' it moves to Man City and then stays there unless they type 'an u' which then changes the list to man united.

    any ideas?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Are you really sure you want to do it that way? The search box will contain "Mu" not "Man U" i could make you a fast binary searh if you want.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50
    my list has about 350 items and i want the user to type in a search and the list moves to the nearest item

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here goes, this binary search should find you as fast as possible, remove the lcases if you want case sensistive search.
    Code:
    Private Sub Text1_Change()
        List1.ListIndex = BinarySearch(Text1.Text, List1)
    End Sub
    
    Function BinarySearch(Byval str As String, list As ListBox)
        Dim x&, l&, temp$
        str = lcase(str)
        l = List1.ListCount / 2
        x = l
        Do
            temp = lcase(list.list(x))
            If str = temp Then
                Exit Do
            ElseIf str > temp Then
                l = l / 2
                x = x + l
            Else
                l = l / 2
                x = x - l
            End If
            
        Loop While l
        BinarySearch = x
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

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



Click Here to Expand Forum to Full Width