(VB.net) Find specified text on an entire line (not just beginning characters) in a listbox
I hav开发者_StackOverflow社区e a listbox where each line contains a short 3-4 character model number followed by a tab and then the product name the model number corresponds to. I also have a textbox which I am using to search the listbox.
The code I am using so far works somewhat, just not exactly how I would like. If I enter search text it will highlight the results in the listbox but only for the first characters, is there anyway to search the text of an entire line (index) of a listbox?
Right now I am using the following:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
ListBox.SelectedIndex = ListBox.FindString(txtSearch.Text)
End Sub
On the assumption that your line representation is the ToString() of the item in the list:
ListBox.SelectedItem.ToString().Contains(txtSearch.Text)
I got it to work by looping through and using contains. Issues with upper and lower case were giving me incorrect results so I converted the criteria to all lower.
For i = 0 To ListBox.Items.Count - 1
If ListBox.Items(i).ToString.ToLower.Contains(Trim(LCase(txtSearch.Text))) Then
ListBox.SelectedIndex = i
Exit For
End If
Next
精彩评论