Prevent double entries in ListView using C#?

前端 未结 6 1991
日久生厌
日久生厌 2021-01-16 09:59

How can we access the items added to a ListView?

The thing I have to do is: add an item to the list view. I want to check if the item to add to the listview is alrea

6条回答
  •  我在风中等你
    2021-01-16 10:32

    The ListView class provides a few different methods to determine if an item exists:

    • Using Contains on the Items collection
    • Using one of the FindItemWithText methods

    They can be used in the following manner:

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

提交回复
热议问题