Get selected column in CListCtrl

How do I find the selected column / subitem in a CListCtrl ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
I am assuming you are talking about report style in the list control.

You need to loop through each item in a row and check the state for LVIS_SELECTED

for(int i = 0; i < XXXX; i++)  //insert number of columns here
  if(pListCtrl->GetItemState(i, LVIS_SELECTED) & LVIS_SELECTED)//pListCtrl is pointer to your list control

This will get the ROW that is selected - should you need it

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

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

ClausAuthor Commented:
I am sorry but doesn't GetItemState(i, xx) return the status of row i, and not column i ??
It seems to do that according to my docs.
AndyAinscowFreelance programmer / ConsultantCommented:
I think you are correct (oops).

If you are responding to a mouse click you could use

LVHITTESTINFO lvhti; = myPoint;

if (lvhti.flags & LVHT_ONITEMLABEL)
  //lvhti.iItem and lvhti.iSubItem have the row/column

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ClausAuthor Commented:
It is not necessarily a mouseclick - it can also be the user typing RETURN.
AndyAinscowFreelance programmer / ConsultantCommented:
No further ideas at present.
ClausAuthor Commented:
The method with SubItemHitTest seems very time consuming and clumsy since I then have to loop through both all rows and columns.

The related articles listed by jaime_olivares contain a wealth of information but did not seem to provide a specific answer to this question.

Anyway, the code below seems to fix it:

            // the row (nItem) is earlier found with GetFirstSelectedItemPosition and
            // GetNextSelectedItemPosition
            LVITEM pItem;
            pItem.iItem = nItem;          
            pItem.mask = LVIF_STATE;
            pItem.stateMask = LVIS_SELECTED;
            for( int i = 0; i < m_CostList.GetColumnCount(); i ++ )
                pItem.iSubItem = i;
                m_CostList.GetItem( &pItem );
                if( pItem.state == LVIS_SELECTED )
                    nSubItem = i;

ClausAuthor Commented:
I request a refund as I answered the question myself.

PAQed with points refunded (125)

EE Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

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.