• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

CListCtrl Item Selection??

The "List Box" in MFC has a member function that returns the index of the item selected. GetCurSel().

Can anyone tell me how to do the same thing with a List View Control(CListCtrl)
0
shark351
Asked:
shark351
  • 2
  • 2
1 Solution
 
chensuCommented:
CListCtrl::GetFirstSelectedItemPosition and CListCtrl::GetNextSelectedItem.

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
    }
}
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Do the following thing to get the selected item
here m_ctrlPacketSignalListView is the control variable for that list control.

int l_iItemCount = m_ctrlPacketSignalListView.GetItemCount();
            
for(int l_iIndex = 0;l_iIndex<l_iItemCount;l_iIndex++)
{
if(m_ctrlPacketSignalListView.GetItemState(l_iIndex,LVIS_SELECTED))
{
//the l_iIndex gives u the selected item index
break;//u can break or continue
}
                  
}

Try it out.
VinExpert
0
 
shark351Author Commented:
Chensu,
I am using VC++ 5.0
Are the functions you mentioned not available for this version??
I tried to implement them and find documentation but failed on both.
0
 
chensuCommented:
I am not sure. I don't have version 5.0 to check. You may search all the source files of MFC for the function names.
0
 
shark351Author Commented:
Chensu,

Must not be available for version 5.0.
I will issue you the points since your answer is technically correct. In the meantime I'm using HitTest() to find which item I've selected.
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now