如何从列表视图中获取所选项目的索引

问题描述:

大家好,

每当我尝试从列表视图中获取所选项目的索引时,第一次就选择了索引,但是第二次每当我从列表视图中选择某些特定项时,就会发生错误,指出参数无效,索引的值为0无效"


我的意思是说可以一次选择,但是第二次我不能从列表视图中选择项目..



例如... ...我在表单上的a上使用了listview控件...将3个项目设为hi,hello和world. MessageBox.Show(选择的索引是" + listView1.SelectedIndices [0]);在selectedindexchanged事件上,因此每当我选择一个项目时,消息框就会出现,但是下次我选择任何其他项目时,都会出现错误提示.."InvalidArgument ="0"的值对于"index"无效. /> 参数名称:索引"


我的意思是说我希望我可以从列表视图中选择任意多次.



有人可以帮助我吗....

hi all ,

whenever i try to get the index of the selected item from the listview , for the first time the index gets selected but second time whenever i select some particular item from listview then error occurs stating "Invalid argument , value of 0 is not valid for index "


I mean to say one time selection is possible but second time i cannot select the item from the listview..



For instance ... I took a listview control on a on the form ... put 3 items hi , hello , world ... now i put a statement
MessageBox.Show("selected index is " +listView1.SelectedIndices[0] ); on the selectedindexchanged event so whenever i select an item messagebox does come but next time whenever i select any other item an error flashes stated .."InvalidArgument=Value of ''0'' is not valid for ''index''.
Parameter name: index"


I mean to say that i want that i can select any number of times a single item from the listview .



CAN anybody help me with that ....

//Assign few data on OnInit to the List
BOOL CLEDDlg::OnInitDialog()
{
m_ctrlListData.AddString("Red");
m_ctrlListData.AddString("Blue");
m_ctrlListData.AddString("Yellow");
...
...
}
/// Write the below code on click of a button

CLEDDlg::OnButton()
{
int n = m_ctrlListData.GetCurSel();
CString szTemp;
szTemp.Format("%d",n);
AfxMessageBox(szTemp);// to display the index selected
}



如果需要的话,请写回信.



Please write back if this is what you needed.