Question How to search a listbox?

fantity

Active member
Joined
Mar 10, 2012
Messages
27
Programming Experience
Beginner
How do I make a search button so I can search for a string in my listbox. Example: The word "Apple" is in the listbox, I type "Apple" into the search box, click search and it shows up with the closest matches.
 
People often ask "can I make a button that does..." like a Button has some special magic. It doesn't. A Button raises a Click event and that's it. That is the sum total of its involvement. If you want to perform an action then you have to write code to perform that action. If you want the action performed when the Button is clicked then you put the code in the Button's Click event handler, but the same code would still perform the same action wherever you put it.

As for the action, the ListBox has FindString and FindStringExact methods that may be useful to you but you've rather muddied the waters by saying:
it shows up with the closest matches
You'll need to provide a definition of what constitutes "closest". You can't be vague at all when it comes to telling a computer what to do.
 
No, you can't be vague in telling what the computer to do but you can be specific about asking it to do a fuzzy search. I am currently working on a word search program and have included a routine to search for all words matching a search pattern in which there is assumed to be one misprint, for example.

If you want to do such a search I would recommend creating a Generic List from your ListBox and using FindAll. MS Help has a walkthrough on the subject. It takes a bit of getting your head around but it's really effective and fast as lightning.
 
People often ask "can I make a button that does..." like a Button has some special magic. It doesn't. A Button raises a Click event and that's it. That is the sum total of its involvement. If you want to perform an action then you have to write code to perform that action. If you want the action performed when the Button is clicked then you put the code in the Button's Click event handler, but the same code would still perform the same action wherever you put it.

As for the action, the ListBox has FindString and FindStringExact methods that may be useful to you but you've rather muddied the waters by saying:You'll need to provide a definition of what constitutes "closest". You can't be vague at all when it comes to telling a computer what to do.

How would I make it search for the word that is in a TextBox. Like, if I type "a" and press search, it displays all the words starting with a.
 
There are various ways that could be achieved but, again, your description is rather vague. What do you mean by "it displays"? Do you mean that the other items are removed from the ListBox? Are they to be displayed in a MessageBox? Something else? When posting a question you must remember that we know nothing about what's in your project or what's in your head, so you need to provide us with all the relevant information.

I would suggest something along the lines of what Dunfiddlin suggested, i.e. using a Predicate, but not quite the same implementation. As you're using .NET 4.0, you may as well us a LINQ query, which can be applied directly to the Items of your ListBox. This will give you a list of matching values that you can do with as you will, e.g.
Dim matches = myListBox.Items.Cast(Of String)().Where(Function(s) s.StartsWith(myTextBox.Text, StringComparison.CurrentCultureIgnoreCase))
That presupposes that the items in the ListBox actually are Strings.
 
There are various ways that could be achieved but, again, your description is rather vague. What do you mean by "it displays"? Do you mean that the other items are removed from the ListBox? Are they to be displayed in a MessageBox? Something else? When posting a question you must remember that we know nothing about what's in your project or what's in your head, so you need to provide us with all the relevant information.

I would suggest something along the lines of what Dunfiddlin suggested, i.e. using a Predicate, but not quite the same implementation. As you're using .NET 4.0, you may as well us a LINQ query, which can be applied directly to the Items of your ListBox. This will give you a list of matching values that you can do with as you will, e.g.
Dim matches = myListBox.Items.Cast(Of String)().Where(Function(s) s.StartsWith(myTextBox.Text, StringComparison.CurrentCultureIgnoreCase))
That presupposes that the items in the ListBox actually are Strings.

All I want to do is have a listbox which has items in it, let's say "apple". If I type any part of "apple" in order and click search, then the listbox will clear and show the closest matches (if I typed "ap" it would list "apple" and other words that start with ap.") and list them in the listbox.
 
You can't simply clear the listbox because that would eliminate the possibility of doing another search. I would use a second listbox to display the results.

Supposing you enter your search criteria in a textbox. Then click a button to find a match. You could do something like this:

VB.NET:
    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        For Each word As String In lstSearch.Items
            If word.Substring(0, 2) = txtSearch.Text Then
                lstFind.Items.Add(word)
            End If
        Next
    End Sub
 
This is why I suggested keeping the whole list as a Generic List and using the listbox for results only. I actually have a piece of code which will require just a few tweaks to do this. You'll have to wait a while though cos it's very nearly bedtime here! :sleeping:
 
In that case I would concur with Dunfiddilin that you should use a generic List as your master and only ever display the results of the filter in the ListBox. I've pretty much already given you the code though, as it's basically the same as what I posted previously. You simply substitute your master list for the Items of the ListBox. You can also drop the Cast call because the List is already generic.
Dim matches = myGenericList.Where(Function(s) s.StartsWith(myTextBox.Text, StringComparison.CurrentCultureIgnoreCase))
You can then simply bind that to the ListBox:
myListBox.DataSource = matches.ToArray()
The ToArray call is required because data-binding requires an IList while matches is only an IEnumerable. Don't worry if that doesn't mean anything to you at present. Just take my word for it.
 
Back
Top