954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search Listbox item

I'm just wondering guys if it is possible for me to go to the specified listbox item via searching. Example;

Listbox1 items are
Apple
Banana
Guava
Pineapple
Peach

When I key in the word Apple in a textbox,
the SelectedItem would go to Apple,

is this possible?

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 
is this possible?


Yes it is.

First, here's a very straightforward solution for the search. You need a list box, text box and a button. Type a search-term in the text box and press the button.

Private Sub SearchListBox(ByVal searchTerm As String)
    ' Fill test data
    ListBox1.Items.Clear()
    ListBox1.Items.Add("Apple")
    ListBox1.Items.Add("Banana")
    ListBox1.Items.Add("Guava")
    ListBox1.Items.Add("Pineapple")
    ListBox1.Items.Add("Peach")

    ' Loop items
    For Each lbItem As Object In ListBox1.Items
        ' Case-sensitive match
        If lbItem.ToString = searchTerm Then
            ' Match found: set as selected item and exit procedure
            ListBox1.SelectedItem = lbItem
            Return
        End If
    Next

End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    '
    SearchListBox(TextBox1.Text)

End Sub


You may have to modify this approach for your specific needs, like case-insensitive search or "search as you type" search.

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Also, see if this helps.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        With ListBox1
            If .Items.Contains(TextBox1.Text) Then .SelectedItem = TextBox1.Text Else .SelectedIndex = -1
        End With
    End Sub
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Your codes guys are awesome. I tested them both, however, they do just the same. How about, making a search that is not case-sensitive? That's what I need here,

Thanks..

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

Not case-sensitive? WTF? You gonna pay extra for that or are you just asking for a freebie? :D

See if this helps.:)

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        With ListBox1
            .SelectedIndex = -1 '// -1 Index ='s No.Item.Selected.
            If Not TextBox1.Text = "" Then
                For Each itm As String In .Items '// loop thru items.
                    If itm.ToLower = TextBox1.Text.ToLower Then '// Case inSensitive search.
                        .SelectedItem = itm '// select item.
                        Exit For '// exit loop since item located. 
                    End If
                Next
                Exit Sub '// just.because the next 2 lines are not worth reading. :)
            End If
        End With
    End Sub
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

As always, this community is really my angel... This thread is solved, thanks

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: