tags:

views:

957

answers:

5

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 already present in the ListView.

I'm using C# and Visual Studio 2005.

+1  A: 

You could do something like this:

 ListViewItem itemToAdd;
 foreach (ListViewItem item in yourListView.Items) 
 {
    bool exists = false;
    if(item == itemToAdd)
        exists=true;
 }

 if(!exists)
     yourListView.Items.Add(itemToAdd);
Robban
A: 

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");
 }
Sauron
A: 

The following will help to locate a ListViewItem within the ListView control once you've added it:

string key = <some generated value that defines the key per item>;
if (!theListViewControl.Items.ContainsKey(key))
{
    item = theListViewControl.Items.Add(key, "initial text", -1);

}

// now we get the list item based on the key, since we already 
// added it if it does not exist
item = theListViewControl.Items[key];
...

Note The key used to add the item to the ListView items collection can be any unique value that can identify the ListViewItem within the collection of items. For example, it could be a hashcode value or some property on an object attached to the ListViewItem.

Mike J
+1  A: 

Just add your items and make sure you assign a name. Then just use the Contains 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
kripto_ash
This seems to be the way to implement the containment test properly. Why did MS not make it clear that item.Name *is* the key? Or better yet, simply implement their own IEquatable interface on the ListView.Items collection? Bah... +1 kripto
dboarman
+3  A: 

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

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);
Ahmad Mageed
Works like charm, why isnt this answer accepted?
Stephen