C# loop through checked items in listview box

blue-coder
blue-coder used Ask the Experts™
on
I have a list view box with two colums (name and address), and I have enabled checkboxes on the listview box.  listview box is named lvDevices.  I would like to loop through each checked item and perfom a task, however when i tried using a standard for each statement, noted below, I got a casting error.  anyone have any suggestions?

            foreach (string cb in lvDevices.CheckedItems)
            {
                do some work
            }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
lvDevices.CheckedItems does not return a set of strings, it returns a collection of ListViewItems, so you need to get each listview item and then get the values from it.  I don't have visual studio on this pc to try, but start with this:
I think it would be:
foreach (ListViewItem lv in lvDevices.CheckedItems)
{
    //Extract data from listview item lv.
}

Author

Commented:
thank you w00te, I was unaware that checkeditems had a different output for listboxes than it did for checklistboxes.  anyway, your pseudo code worked great.  thanks for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial