title: Prevent duplicate items from being added to a ListBox
body: <p>I have this code for adding selected items from one <code>ListBox</code> to another. How can I prevent the user from adding an item twice?  I want the <code>ListBox</code> they are adding to <code>lstBoxToUserProjects</code> to only contain distinct items with no duplicate entries.</p>
  
  <pre><code>protected void btnAddSelectedItem_Click(object sender, EventArgs e)
  {
      List&lt;ListItem&gt; itemsToAdd= new List&lt;ListItem&gt;();
  
      foreach (ListItem listItem in lstbxFromUserProjects.Items)
      {
          if (listItem.Selected)
              itemsToAdd.Add(listItem);
      }
  
      foreach (ListItem listItem in itemsToAdd)
      {
          lstBoxToUserProjects.Items.Add(listItem);
      }
  }
  </code></pre>
  
  <p><strong>EDIT:
  Here's what I ended up using</strong></p>
  
  <pre><code>protected void btnAddSelectedItem_Click(object sender, EventArgs e)
  {
      List&lt;ListItem&gt; itemsToAdd= new List&lt;ListItem&gt;();
  
      foreach (ListItem listItem in lstbxFromUserProjects.Items)
      {
          if (listItem.Selected)
              itemsToAdd.Add(listItem);
      }
  
      foreach (ListItem listItem in itemsToAdd)
      {
  
          if (!lstBoxToUserProjects.Items.Contains(listItem)) 
          {
              lstBoxToUserProjects.Items.Add(listItem);
          }
      }
  }
  </code></pre>