C# 防止重复项添加到列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9419905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent duplicate items from being added to a ListBox
提问by Ronald McDonald
I have this code for adding selected items from one ListBoxto another. How can I prevent the user from adding an item twice? I want the ListBoxthey are adding to lstBoxToUserProjectsto only contain distinct items with no duplicate entries.
我有此代码用于将所选项目从一个添加ListBox到另一个。如何防止用户两次添加项目?我希望ListBox他们添加的内容lstBoxToUserProjects仅包含没有重复条目的不同项目。
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);
}
}
}
采纳答案by kaj
If you bind the lstBoxToUserProjectslist 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:
如果您将lstBoxToUserProjects列表框绑定到数据源 (HashSet),那么您可以做一个简单的检查以查看建议选择的项目是否已经在目标中:
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.
注意我建议使用 HashSet,因为这样您就可以对集合进行性能检查,而必须枚举 List 以检查匹配项。
回答by gdoron is supporting Monica
Change itemsToAddfrom Listto HashSet:
改变itemsToAdd来自List于HashSet:
HashSet<ListItem> itemsToAdd= new HashSet<ListItem>();
...
itemsToAdd.Add(listItem) // Adds only new items.
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...)
返回值
类型:System.Boolean 如果将元素添加到 HashSet(Of T) 对象,则为 true;如果元素已经存在,则为 false。(gdoron-并且不再插入元素......)
回答by cain
You should just call ListBox.Items.Contains() in an if statement to check if it has already been added.
您应该只在 if 语句中调用 ListBox.Items.Contains() 来检查它是否已添加。
foreach (ListItem listItem in itemsToAdd)
{
if (!lstBoxToUserProjects.Items.Contains(listItem))
{
lstBoxToUserProjects.Items.Add(listItem);
}
}
回答by Krizz
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.
这至少假设 C# 3.5。
回答by Raman Singh
Use
用
_items_Unique = _items.Distinct().ToList();
method it's fast then comparing where _items_Unique and _items are two list
方法它很快然后比较 _items_Unique 和 _items 是两个列表
List<string> _items_Unique = new List<string>();
List<string> _items = new List<string>();

