Prevent double entries in ListView using C#?

前端 未结 6 1853
日久生厌
日久生厌 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:38

    A small correction in Robban's answer

     ListViewItem itemToAdd;
     bool exists = false;
     foreach (ListViewItem item in yourListView.Items)
     {
        if(item == itemToAdd)
        {
           exists=true; 
           break; // Break the loop if the item found.
        }
     }
     if(!exists)
     {
        yourListView.Items.Add(itemToAdd);
     }
     else
     {
        MessageBox.Show("This item already exists");
     }
    

提交回复
热议问题