使用C#阻止ListView中的双重条目?

Bin*_*inu 1 .net c# listview

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

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

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

Ahm*_*eed 17

本ListView类提供了几种不同的方法来确定一个项目存在:

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

// 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);
Run Code Online (Sandbox Code Playgroud)

  • 像魅力一样工作,为什么这个答案不被接受? (3认同)