Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the index of selected item of a CListCtrl 1

Status
Not open for further replies.

scottmanjoe

Technical User
Mar 28, 2003
15
IN
Hello,
How to get the index of the selected items of a CListCtrl Control Object.
GetSelectedCount() will give the number of selected items.
and GetNextItem() will give the next selected item(with the LVNI_SELECTED flag).

But i am not able to implement it and after one loop it gives a value -1 for the selected item.

int iCount = m_listServices.GetSelectedCount();
int nIndex = -1;
for(int i=0;i<iCount;i++)
{
nIndex = m_listServices.GetNextItem(nIndex, LVNI_SELECTED);
if(nIndex==-1)
break;
...//use iIndex to do something
}

Thanks,
Scott
 
Use GetFirstSelectedItemPosition() and GetNextSelectedItem() to iterate through the selected items:

CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
AfxMessageBox(&quot;No items selected!&quot;);
else
{
while (pos)
{
int index = pList->GetNextSelectedItem(pos);
AfxMessageBox(&quot;Item %d was selected!\n&quot;, index);
//...
}
}

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top