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