Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How prevent duplicate items listView C#

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

ListViewItem lvi = new ListViewItem();
lvi.Text = comboBox1.Text;
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("")

if (!listView1.Items.Contains(lvi))
{
    listView1.Items.Add(lvi);
}

I need prevent duplicate items but not work, How Can I solve this?

like image 966
Vincenzo Lo Palo Avatar asked Mar 08 '13 09:03

Vincenzo Lo Palo


1 Answers

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);
like image 96
Parimal Raj Avatar answered Oct 06 '22 00:10

Parimal Raj