Find a list of items between 2 listboxes vb.net
i want to check for each item in list box if item exist then add it to listbox3
for each item in listbox2.items
if listbox1.items.contains (item) then
listbox3.item.add (item)
end if
next
but it is very slow if there is way to search for list of items in开发者_C百科 listbox at once?
You could use the Intersect method to get all the items they both contain. http://msdn.microsoft.com/en-us/library/bb460136.aspx
It would look something like. Code below is untested.
For Each item in listbox2.items.Intersect(listbox1.items)
listbox3.item.add (item)
Next
EDIT: So after looking at it, it seems items is an objectcollection which doesn't contain intersect. To get around that you can try.
For Each item in listbox2.items.Cast(Of Enumerable).Intersect(listbox1.items.Cast(Of Enumerable))
listbox3.item.add (item)
Next
Not sure how fast that will be though
Unless each Listbox's collection of items is of a large magnitude, I don't see anything wrong with that approach (besides the minor coding errors).
I created a list of 90,000 strings and this worked instantly:
ListBox3.DataSource = ListBox1.Items.Cast(Of IEnumerable).Intersect(ListBox2.Items.Cast(Of IEnumerable)).ToList
精彩评论