我正在使用 Windows 窗体
。使用此代码,我将项目从 comboBox
添加到 listView
。
ListViewItem lvi = new ListViewItem();
lvi.Text = comboBox1.Text;
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("")
if (!listView1.Items.Contains(lvi))
{
listView1.Items.Add(lvi);
}
我需要防止重复项但不起作用,我该如何解决?
最佳答案
ListView 类提供了几种检查项目是否存在的方法:
它可以像这样使用:
// assuming you had a pre-existing item
ListViewItem item = ListView1.FindItemWithText("item_key");
if (item == null)
{
// item does not exist
}
// you can also use the overloaded method to match subitems
ListViewItem item = ListView1.FindItemWithText("sub_item_text", true, 0);
关于c# - 如何防止重复项目listView C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290779/