Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get selected column in CListCtrl

Posted on 2004-11-01
11
Medium Priority
?
1,755 Views
Last Modified: 2013-11-20
How do I find the selected column / subitem in a CListCtrl ?
0
Comment
Question by:Claus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 12463068
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
ID: 12463126
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 45

Expert Comment

by:AndyAinscow
ID: 12463216
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Expert Comment

by:AndyAinscow
ID: 12463413
No further ideas at present.
0
 

Author Comment

by:Claus
ID: 12470456
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
ID: 12816881
I request a refund as I answered the question myself.

Thanks,
Claus
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 15705007
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

618 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