2011-01-01 4 views
0

ImageList의 이미지를 보여주는 ListView가 있습니다. 이제 ListView에서 검사 된 모든 이미지의 인덱스를 가져 오려고했습니다.listview, checkbox, C#

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 ]; 
    } 
} 

답변

3

ListView에는 이미 CheckedIndices 속성이 있습니다. 아마 해야지 직접 사용하는,하지만 당신은 Linq에 한 라이너와 함께 밖으로 목록 <>을 얻을 수 있습니다 :

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

글쎄, 난 완전히 당신의 질문을 이해 모르겠지만, 당신은 item.Index과 함께있는 ListViewItem의 인덱스를 얻을 수 있습니다.

0
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]); 
} 
+0

내가 이미 시도했지만 목록 가 유효하지 않은 매개 변수가 가장 과부하 일치하는 방법을 제공됩니다. – Shahgee

+0

내가 이미 시도했지만 목록에 대한 오버로드 된 일치 방법이 가장 좋음 에 잘못된 매개 변수가 있습니다. 내가 messagebox에 보여주고 싶을 때 나는 이유를 이해하지만 donot은 그것을 고치는 법을 안다. MessageBox.Show (lstview1.Items [item.Index] .ToString()); // listviewitem = {}을 보여줍니다. – Shahgee