使用 C# 防止 ListView 中出现重复条目​​?

2023-12-10

我们如何访问添加到的项目ListView?

我要做的事情是:将一个项目添加到列表视图中。我想检查要添加到列表视图的项目是否已存在于 ListView 中。

我正在使用 C# 和 Visual Studio 2005。


The ListView类提供了几种不同的方法来确定项目是否存在:

  • Using Contains on the Items收藏
  • 使用其中之一FindItemWithText methods

它们可以按以下方式使用:

// assuming you had a pre-existing item
ListViewItem item = ListView1.FindItemWithText("test");
if (!ListView1.Items.Contains(item))
{
    // doesn't exist, add it
}

// or you could find it by the item's text value
ListViewItem item = ListView1.FindItemWithText("test");
if (item != null)
{
    // it exists
}
else
{
    // doesn't exist
}

// you can also use the overloaded method to match sub items
ListViewItem item = ListView1.FindItemWithText("world", true, 0);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 C# 防止 ListView 中出现重复条目​​? 的相关文章

随机推荐