How prevent duplicate items listView C#

后端 未结 5 697
终归单人心
终归单人心 2020-12-19 07:50

I am using Windows Forms. With this code I add items to listView from comboBox.

ListViewItem lvi = new ListViewItem();         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 08:17

    The ListView class provides a few way to check if an item exists:

    • Contains On Items collection, and
    • FindItemWithText methods

    It can be used like :

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

提交回复
热议问题