Retrieving selected items


To retrieve the index of the first selected item use GetNextItem(). As its name implies, you can use also use the GetNextItem() function to retrieve the index of the next selected item. The code below prints out the indices of all selected items to the debug window.
// This code prints out the indices of selected items to debug win
 int i = GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
 while( i != -1 )
 {
	TRACE("%d\n", i );
	i = GetNextItem( i, LVNI_ALL | LVNI_SELECTED);
 }
 



Comments

  • some modification

    Posted by Legacy on 04/21/2002 12:00am

    Originally posted by: Alexey Popov

    int start=-1;
    
    

    do
    {
    start=ListView_GetNextItem(m_hWndListView, start, LVNI_SELECTED);
    if (start!=-1)
    {
    // put your code here
    }
    else
    {
    break;
    }
    }
    while (/*start != -1*/ true);

    Reply
  • SetItemData Works fine for me with a Dlg

    Posted by Legacy on 01/26/2001 12:00am

    Originally posted by: David Smulders

    m_List.SetItemData(nRow, reinterpret_cast<DWORD>(new CString(m_Text)));
    
    


    void CMyDlg::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    if (pNMListView && pNMListView->uOldState != pNMListView->uNewState)
    {
    if ((pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
    {
    CString* pString = reinterpret_cast<CString*>(m_List.GetItemData(pNMListView->iItem));

    if (pString)
    {
    m_Output= *pString;
    UpdateData(FALSE);
    }
    }
    }

    *pResult = 0;
    }

    Reply
  • Example did not work; but MSDN solution did

    Posted by Legacy on 02/06/2000 12:00am

    Originally posted by: Gerard Nicol

    When using the defined solution; loop kept returning item 0.
    
    Although, in theory it should have worked.

    The solution from MSDN is:

    CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
    ASSERT(pListCtrl != NULL);

    POSITION pos = pList->GetFirstSelectedItemPosition();
    if (pos == NULL)
    TRACE0("No items were selected!\n");
    else
    {
    while (pos)
    {
    int nItem = pList->GetNextSelectedItem(pos);
    TRACE1("Item %d was selected!\n", nItem);
    // you could do your own processing on nItem here
    }
    }

    This did work.

    Reply
  • So simple, yet so perfect . . .

    Posted by Legacy on 09/15/1999 12:00am

    Originally posted by: Andon M. Coleman

    Good work yet again, Zafir :)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds