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?
The ListView class provides a few way to check if an item exists:
Contains
On Items collection
, andFindItemWithText
methodsIt 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With