Filter Combobox items

topsykretts

Well-known member
Joined
Dec 22, 2011
Messages
47
Programming Experience
Beginner
Is there a way to filter combobox items (declared as single) to find which values are greater than an specific given value?
 
Yes and no. It's quite easy to filter a list of data but there's nothing in the ComboBox control that will allow you to display a subset of the items it contains. You have to give the subset to the ComboBox in the first place. I would suggest that you put all the values into an array or collection and keep that as your source. Whenever you need to display a subset you filter that list and pass the result to the ComboBox, e.g.
VB.NET:
Dim subset = fullSet.Where(Function(n) n > someValue).ToArray()

myComboBox.DataSource = subset
That code uses a bit of LINQ to get items from 'fullSet' where that item is greater than 'someValue'. Without LINQ, you could do something like this:
VB.NET:
myComboBox.Clear()

For Each n In fullSet
    If n > someValue Then
        myComboBox.Items.Add(n)
    End If
Next
 
Back
Top