Prevent double entries in ListView using C#?

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

    Just add your items and make sure you assign a name. Then just use the ContainsKey method of the Items collection to determine if it's there, like this.

    for (int i = 0; i < 20; i++)
    {
        ListViewItem item = new ListViewItem("Item" + i.ToString("00"));
        item.Name = "Item"+ i.ToString("00");
        listView1.Items.Add(item);
    }
    MessageBox.Show(listView1.Items.ContainsKey("Item00").ToString()); // True
    MessageBox.Show(listView1.Items.ContainsKey("Item20").ToString()); // False
    

提交回复
热议问题