Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Getting current selection of list control

Posted on 1999-06-22
Medium Priority
Last Modified: 2013-11-20
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
Question by:Slarti
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

Accepted Solution

Shay050799 earned 300 total points
ID: 1307720
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


Expert Comment

ID: 1307721
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LISTCONTROL);

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


Author Comment

ID: 1307722
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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 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