LISTVIEW CHECKEDITEMS FIND ITEM BY NAME

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
C# 
C#
 1506  View(s)
Ratings:
Rate this:

I need to check if an item with a particular name exists in the CheckedItems collection of a ListView.

So far I've tried:

ListViewItem item = new ListViewItem(itemName);

if (listView1.CheckedItems.IndexOf(item) >= 0)

   return true;

and

ListViewItem item = new ListViewItem(itemName);

if (listView1.CheckedItems.Contains(item))

   return true;

Neither of those worked. Is there a way to do this without looping through CheckedItems and checking them one by one?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: ListView CheckedItems find item by name

    Hi Jayden,

    Get rid of newing up a ListViewItem and do this instead:

    ListViewItem itemYouAreLookingFor = listView1.FindItemWithText("NameToLookFor");

    // Did we find a match?

    if (itemYouAreLookingFor != null)

    {

        // Yes, so find out if the item is checked or not?

        if(itemYouAreLookingFor.Checked)

        {

            // Yes, it is found and check so do something with item here

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!