[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1857
  • Last Modified:

Get selected column in CListCtrl

How do I find the selected column / subitem in a CListCtrl ?
0
Claus
Asked:
Claus
1 Solution
 
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
    AfxMessageBox("SELECTED");




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");
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
 
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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I think you are correct (oops).

If you are responding to a mouse click you could use

LVHITTESTINFO lvhti;
lvhti.pt = myPoint;
pListCtrl->SubItemHitTest(&lvhti);

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

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ClausAuthor Commented:
It is not necessarily a mouseclick - it can also be the user typing RETURN.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
No further ideas at present.
0
 
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;
                    break;
                }
            }

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

Thanks,
Claus
0
 
Computer101Commented:
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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