show results from a listbox only?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: show results from a listbox only?

  1. #1
    Join Date
    Jan 2012
    Posts
    27

    show results from a listbox only?

    Lets say I have a textbox, and a listbox, and this listbox contains like 300 items, with everything from "apple" to "blue", and lets say you want to look for a particular word, but you dont know the full thing, or maybe you do, but you dont want to type in "supercalafraglisticespealadouchus", so you just type in "supe" and chances are, its going to return maybe 5 items that contain the exact words "supe" in it, and "supercala..." is one of them...

    In other words: How would I make the listbox refresh with the new results that only contain those words? I have it set to check what is typed, and display a messagebox, and then clear the list, but I dont know the code to show the results in the listbox that only contain those words.

    Heres my code for detecting the words in the listbox:
    Code:
    Dim files = IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Desktop)
            Dim q = From file In files _
                  Select IO.Path.GetFileName(file)
    
            For Each item In currentdirectorysearch.Items
                If currentdirectorysearch.FindString(searchbar.Text) = True Then
                    MessageBox.Show("That aint even what u typed...")
                Else
                    MessageBox.Show("Thats... Actually what you typed")
                End If
            Next
    (currentdirectorysearch is the listbox, searchbar is the textbox)

    Heres my code to populate the listbox, when the application loads:
    Code:
    Dim files = IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Desktop)
            Dim q = From file In files _
                  Select IO.Path.GetFileName(file)
    
            currentdirectorysearch.Items.Clear()
            currentdirectorysearch.Items.AddRange(q.ToArray)
            Dim i As Long
            Dim j As Long
            Dim List1
            With List1
                For i = 0 To .ListCount - 1
                    For j = .ListCount To (i + 1) Step -1
                        If .List(j) = .List(i) Then
                            .RemoveItem(j)
                        End If
                    Next
                Next
            End With

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,830

    Re: show results from a listbox only?

    One simple way would be to use 2 listboxes. 1 with all the words and 1 that will be rebuilt on the fly

    So when the user has typed something you clear the second listbox [which should be invisible]
    You loop through the first list and add any matching items to the second list and then show the second list while hiding the first one.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: show results from a listbox only?

    It is actually quite simple As DM also mentioned.

    I have added two listboxes and one TextBox.

    ListBox1 I populated with the 7 Archangles :

    Raphael
    Michael
    Gabriel
    Uriel
    Remiel
    Raguel
    Saraqael

    ListBox2 is empty

    In the TextBox's TextChanged event I added the following :

    Code:
    	Private Sub TextBox1_TextChanged( sender As Object,  e As EventArgs) Handles TextBox1.TextChanged
    		ListBox2.Items.clear
    		
    		For i As Integer = 0 To listBox1.Items.Count - 1
    	
    		
    			If listBox1.GetItemText(listBox1.Items(i)).StartsWith(textBox1.Text) Then
    	
    				listBox2.items.Add(ListBox1.Items(i))
    	
    			End If
    	
    		Next
    
    	End Sub
    Now, whenever I type R in there I get :

    Raguel
    Raphael
    Remiel

    in ListBox 2

    Is that what you're after?

    If you wanted to autocomplete your TextBox1 text with the items based in ListBox 1, you could do this as well :

    Code:
        Private Sub UpdateAutoComplete()
    
          '  Clear current autocomplete list
            textbox1.AutoCompleteCustomSource.Clear()
    
           ' Loop through each listbox item and add it to the Autocomplete source
            For i As Integer = 0 To listbox1.Items.Count - 1
                textbox1.AutoCompleteCustomSource.Add(listbox1.Items(i))
            Next
    
    
        End Sub
    	
    	Private Sub Form1_Load( sender As Object,  e As EventArgs) Handles MyBase.Load
    		
    		UpdateAutoComplete
    
    	End Sub
    Does that help?

    Hannes

  4. #4
    Join Date
    Jan 2012
    Posts
    27

    Re: show results from a listbox only?

    Thank you both for the help

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center