+ Reply to Thread
Results 1 to 2 of 2

Thread: disallow duplicate items from being added

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    15

    disallow duplicate items from being added

    I have a listbox and i`d like to prevent duplicate items from being added to listbox , here is what i wrote so far :
    Code:
        List<ListItem> li= new List<ListItem>();
    
        foreach (ListItem listItem in lstbxFromUserProjects.Items)
        {
            if (listItem.Selected)
                li.Add(listItem);
        }
    
        foreach (ListItem listItem in li)
        {
            lstBoxToUserProjects.Items.Add(listItem);
        }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    35
    You can use that method , which checks if tthe iteam that will be added is already on the listbox or not :
    Code:
    foreach (ListItem listItem in li)
    {
        if (!lstBoxToUserProjects.Items.Contains(listItem))
        {
            lstBoxToUserProjects.Items.Add(listItem);
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts