Prevent duplicate items from being added to a ListBox

狂风中的少年 提交于 2019-12-04 23:07:58

问题


I have this code for adding selected items from one ListBox to another. How can I prevent the user from adding an item twice? I want the ListBox they are adding to lstBoxToUserProjects to only contain distinct items with no duplicate entries.

protected void btnAddSelectedItem_Click(object sender, EventArgs e)
{
    List<ListItem> itemsToAdd= new List<ListItem>();

    foreach (ListItem listItem in lstbxFromUserProjects.Items)
    {
        if (listItem.Selected)
            itemsToAdd.Add(listItem);
    }

    foreach (ListItem listItem in itemsToAdd)
    {
        lstBoxToUserProjects.Items.Add(listItem);
    }
}

EDIT: Here's what I ended up using

protected void btnAddSelectedItem_Click(object sender, EventArgs e)
{
    List<ListItem> itemsToAdd= new List<ListItem>();

    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);
        }
    }
}

回答1:


If you bind the lstBoxToUserProjects list box to a datasource (HashSet) then you could do a simple check to see if the item proposed for selection was already in the destination:

foreach(ListItem itemToAdd in itemsToAdd)
{
    if (selectedItems.Contains(itemToAdd)) continue;
    lstBoxToUserProjects.Items.Add(itemToAdd);
}

Note I'm proposing a HashSet because then you can do a performant check on the set whereas a List would have to be enumerated to check for a match.




回答2:


You should just call ListBox.Items.Contains() in an if statement to check if it has already been added.

foreach (ListItem listItem in itemsToAdd)
{
    if (!lstBoxToUserProjects.Items.Contains(listItem))
    {
        lstBoxToUserProjects.Items.Add(listItem);
    }
}



回答3:


Try this:

protected void btnAddSelectedItem_Click(object sender, EventArgs e)
{
    lstBoxToUserProjects.Items.AddRange(lstbxFromUserProjects.Items.Where(li => !lstBoxToUserProjects.Items.Contains(li)).ToArray());
}

This assumes C# 3.5, at least.




回答4:


Change itemsToAdd from List to HashSet:

HashSet<ListItem> itemsToAdd= new HashSet<ListItem>();

...
itemsToAdd.Add(listItem) // Adds only new items.

Add MSDN:

Return Value

Type: System.Boolean true if the element is added to the HashSet(Of T) object; false if the element is already present. (gdoron- and not inserting the element again...)




回答5:


Use

_items_Unique = _items.Distinct().ToList();

method it's fast then comparing where _items_Unique and _items are two list

List<string> _items_Unique = new List<string>();
List<string> _items = new List<string>();


来源:https://stackoverflow.com/questions/9419905/prevent-duplicate-items-from-being-added-to-a-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!