[RESOLVED] Search all items in a ComboBox based on a Search Criteria
I have three controls on my form; Textbox1, cboCity, lstCity
When something (lets say "a") is entered in the TextBox1; I want to get each and every item in combobox cboCity that starts with "a" and add them to listbox lstCity.
How do I do it?
Re: Search all items in a ComboBox based on a Search Criteria
How exactly was the ComboBox populated in the first place? Are the items just Strings added manually or have you maybe bound a DataTable or the like.
Re: Search all items in a ComboBox based on a Search Criteria
the combobox is unbound and filled manually (cboCity.items.Add(sCity))
Re: Search all items in a ComboBox based on a Search Criteria
In that case, your best bet is a simple LINQ query in the TextChanged event handler of the TextBox:
vb.net Code:
myListBox.DataSource = myComboBox.Items.Cast(Of String)().
Where(Function(s) s.StartsWith(myTextBox.Text)).
ToArray()
Note that that will be case-sensitive. The StartsWith method also supports case-insensitivity, so check the documentation or even just use Intellisense if that's what you want.
Re: Search all items in a ComboBox based on a Search Criteria
this is why i lurk in this forum.. *takes notes*
Re: Search all items in a ComboBox based on a Search Criteria
Thanks... It worked great.
I just had to add StringComparison.InvariantCultureIgnoreCase t o ignore the case.