Results 1 to 4 of 4

Thread: Sorting through a list box

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    40

    Question Sorting through a list box

    Here's the situation, I have a listbox that contains names in this format "last name, first name". I also have a textbox that the user types can type a string to pull up the appropiate name, i.e. if the user types in "S" I want the listbox to move to the "S" section, or even better only display names that start with "S".

    "Smith, Ro" would bring up "Smith, Robert" (or any other Smith with a first name that starts with "Ro". I'm not sure how I can accomplish this and not kill program response time. The listbox could contain any number of items (probably less than 100, but upwards of 100 is possible.

    Another question, how many items can a listbox hold without a serious slow down in response time?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It seems like there should be some form of intrinsic way to do this, but I only found how to find an exact match (of course I didn't look THAT hard). But if you don't find one then this works just fine:
    VB Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'make an array and fill listbox
    3.         Dim items() As String = {"Marquez, Ed", "Marcus, Allen", "Sarver, Mike", "Doran, Cookie", "Dorn, Mike"}
    4.         ListBox1.Items.AddRange(items)
    5.     End Sub
    6.  
    7.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    8.         'convert listbox item to string array and pass to function
    9.         Dim itms(ListBox1.Items.Count - 1) As String
    10.         ListBox1.Items.CopyTo(itms, 0)
    11.         ListBox1.SelectedIndex= ClosestIndexOf(TextBox1.Text, itms, False)
    12.     End Sub
    13.  
    14.     Private Function ClosestIndexOf(ByVal Text As String, ByVal Items() As String, Optional ByVal CaseSensitive As Boolean = True) As Integer
    15.         'returns -1 if no match at all is found
    16.         Dim itm As String
    17.         Dim found As Integer = -1
    18.         For Each itm In Items
    19.             If CaseSensitive Then
    20.                 If itm.StartsWith(Text) Then
    21.                     found = Array.IndexOf(Items, itm)
    22.                     Exit For
    23.                 End If
    24.             Else
    25.                 'make both lower case before testing
    26.                 Dim tmp As String = itm.ToLower
    27.                 If tmp.StartsWith(Text.ToLower) Then
    28.                     found = Array.IndexOf(Items, itm)
    29.                     Exit For
    30.                 End If
    31.             End If
    32.         Next
    33.         Return found
    34.     End Function

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    40

    thanks

    Thank You!
    Actually it is finding the closest match, i.e. Smi would go to Smith. Thanks for the code sample.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah I wrote that because I figured it'd come in handy for me too at some point, but the intrinsic IndexOf function will find an exact match if you need that.

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