|
-
Feb 8th, 2012, 09:57 PM
#1
Thread Starter
Member
[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?
-
Feb 8th, 2012, 10:09 PM
#2
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.
-
Feb 8th, 2012, 10:13 PM
#3
Thread Starter
Member
Re: Search all items in a ComboBox based on a Search Criteria
the combobox is unbound and filled manually (cboCity.items.Add(sCity))
-
Feb 8th, 2012, 10:22 PM
#4
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.
-
Feb 8th, 2012, 10:29 PM
#5
Addicted Member
Re: Search all items in a ComboBox based on a Search Criteria
this is why i lurk in this forum.. *takes notes*
-
Feb 8th, 2012, 10:59 PM
#6
Thread Starter
Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|