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?

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

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

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..

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
commented: Thanks! +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.