Getting current selection of list control

I'm using MFC in Visual C++ 5.

I have a dialog with a list control in report mode. The dialog also has a static text which is supposed to display additional information about the currently selected item. The list control is set to single selection, but there does not seem to be any simple way to retrieve the current selection. How do I get the index of the currently selected item?

I would appreciate some source code.

I have previously used a list box, in which such a function was available, but now I switched over to a list control and can't figure out how to get this done.

Thanks, Slarti
Who is Participating?
Shay050799Connect With a Mentor Commented:
do the following:
override the message of the listcontrol  =NM_DBCLICK

in that function fo the following:
cast pNMUpDown like this:

int pos=pNMUpDown->iPos;

now pos points to the current selected item

u can do this to retrieve the text

CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LISTCONTROL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos != NULL){
      int iSelIndex = pList->GetNextSelectedItem(pos);
      // nothing selected

SlartiAuthor Commented:
Shay, your answer works. However, it would be more correct to case the structure to type NM_LISTVIEW, which is the structure for list controls and contains more information about the list view. The NM_LISTVIEW's member iItem happens to be in the same position as NM_UPDOWN's iPos, which is why it works. However, NM_UPDOWN refers, of course, to up-down (a.k.a. spin) controls.

gatkinso, I actually like your suggestion better, but it has one problem: it doesn't work, at least on my version -- there is no function "GetFirstSelectedItemPosition".

Thanks to both of your for the fast replies.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.