Shahgee
Shahgee

Reputation: 3405

listview, checkbox, c#

I have ListView which shows images from an ImageList. Now wanted to get index of all checked images in ListView.

List<int> list = new List<int>(); // in list index of all checked images on clicking button should be saved.

private void button2_Click(object sender, EventArgs e)
{
    ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;

    foreach (ListViewItem item in checkedItems)
    {
      list.add[// How can i get index of checked item ];
    }
}

Upvotes: 0

Views: 2127

Answers (3)

Hans Passant
Hans Passant

Reputation: 941327

ListView already has the CheckedIndices property. You probably ought to use it directly, but you can get a List<> out of it with a Linq one-liner:

        var list = listView1.CheckedIndices.Cast<int>().ToList();

Upvotes: 3

Nithin Philips
Nithin Philips

Reputation: 330

ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;

foreach (ListViewItem item in checkedItems)
{
      // This will fill the list with ListViewItems that are checked
      list.add(listview1.Items[item.Index]);
}

Upvotes: 0

corvuscorax
corvuscorax

Reputation: 5930

Well, I'm not sure I understand your question completely, but you can get the index of a ListViewItem with item.Index.

Upvotes: 0

Related Questions