Solved

Get selected column in CListCtrl

Posted on 2004-11-01
1,408 Views
Last Modified: 2013-11-20
How do I find the selected column / subitem in a CListCtrl ?
0
Question by:Claus
    9 Comments
     
    LVL 43

    Expert Comment

    by:AndyAinscow
    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
     

    Author Comment

    by:Claus
    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
     
    LVL 43

    Expert Comment

    by:AndyAinscow
    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
     

    Author Comment

    by:Claus
    It is not necessarily a mouseclick - it can also be the user typing RETURN.
    0
     
    LVL 43

    Expert Comment

    by:AndyAinscow
    No further ideas at present.
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    0
     

    Author Comment

    by:Claus
    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
     

    Author Comment

    by:Claus
    I request a refund as I answered the question myself.

    Thanks,
    Claus
    0
     
    LVL 1

    Accepted Solution

    by:
    PAQed with points refunded (125)

    Computer101
    EE Admin
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now