how is it possible to delete double-entries from a ListBox? Let's say I've got the following Listbox:
5
4
6
4
7
5
As a result I want to have this one:
5
4
6
7
Thank you for your help.
Well, what's your data source? Do you have the data directly in the Items collection, or is it bound to a "normal" collection?
Using LINQ, it's dead easy to get the distinct elements, with something like this:
elements = elements.Distinct().ToList();
(Calling ToList
means that the distinct-ness will only be computed once, rather than every time the underlying infrastructure decides to enumerate the collection. I don't know the details of how that would work with binding, so I typically take a conservative approach and materialize the query results.)
while adding your items to list, u use loop to add.
foreach(int item in yourList)
{
if(!listBox1.Items.Contains(item))
{
//add
}
}
this is the way to add unique numbers if you use direct datasource, Jon Skeet's answer is for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.