tags:

views:

40

answers:

2

Hello,

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.

+1  A: 

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.)

Jon Skeet
+1  A: 

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.

Serkan Hekimoglu