CAD
CAD

Reputation: 4312

How to avoid duplicates when adding items to a list box in C#

I have a list boxes List1 and List2 and I want to add selected item from List2 to List1. I do as follows.

List1.Items.Add(List2.SelectedItem);

How to avoid duplicating in List1 if the same item from List2 is being added?

Upvotes: 0

Views: 118

Answers (1)

TaW
TaW

Reputation: 54463

if (List2.SelectedItem != null 
  if (! List1.Items.Contains(List2.SelectedItem))
     {  List1.Items.Add(List2.SelectedItem);
        List2.Remove(List2.SelectedItem);
     }

Upvotes: 1

Related Questions