Reputation: 3405
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
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
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
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