Results 1 to 10 of 10

Thread: Searching a listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281

    Searching a listbox

    I am writing a program that should have the ability to search through the contents of a list box. I would like to set it up so that as you type text into a textbox it compares that text to the text in the listbox. If the text in the list box does not partain to the text in the textbox it want it removed. And when the text box it cleared I want the listbox to be restored to the original contents.

    I know this is kind of confusing but if anyone can help me I would appreciate it.

    Thanks,
    Mark

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Is this what you're after?
    VB Code:
    1. Dim i As Long
    2.  
    3. For i = List1.ListCount - 1 To 0 Step -1
    4.     If UCase(Text1.Text) <> UCase(List1.List(i)) Then
    5.         'remove it
    6.         List1.RemoveItem i
    7.     End If
    8. Next i

  3. #3
    Member
    Join Date
    Sep 2001
    Location
    USA
    Posts
    33
    you mean kinda like in a help index? as you put in each additional letter the index jumps to the next closest match in the list? what I can suggest is when a key is pressed in the text box, go through each item in listbox and see if the text in the textbox exists in each line of the list box (maybe use InStr?) and if it's not on the line then ListBox.Remove ListBox.ListIndex or something like that. I'm sure there is a better way to do it but that's my 2 cents.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281
    I want it so that if I type the Letter "E" it will eliminate everything that does not begin with the letter "e" or "E". I know how to do the eliminating part but I don't know how to do the comparing part.

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Small modification to code I already gave. Would be an idea to explain yourself properly
    VB Code:
    1. Dim i As Long
    2.  
    3. For i = List1.ListCount - 1 To 0 Step -1
    4.     If UCase(Left$(Text1.Text, 1)) <> UCase(Left$(List1.List(i), 1)) Then
    5.         'remove it
    6.         List1.RemoveItem i
    7.     End If
    8. Next i

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281
    that one gave me an compile error that says "Type-declaration character does not match declared data type"

  7. #7
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    How are you initially populating the listbox?

    If from a database table, then do a SQL Select query based on your entered text in the textbox, this will allow the list box only to be populated with data for each added (or deleted) keystroke.
    Code:
    "SELECT myDispValue FROM myTable WHERE myDispValue LIKE '" &  myText & "%'"
    You must clear the listbox before each re population of the resulting recordset.
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  8. #8
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Any specific line?

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Works fine...

    Example using above code...

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281
    It works great now.....I changed a couple of things and the error went away and it works. Thanks

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