SingleClick Selection in CListCtrl

Posted on 1998-09-23
Last Modified: 2013-11-20
I have a splitter window with two panes - one is a CListView and the other is a CFormView.  

 I would like the CListView to notify the CFormView whenever a list item is selected by a SINGLE CLICK.    I have this working for a DOUBLE CLICK by overriding  WM_LBUTTONDBLCLICK.   I tried overriding WM_ONLBUTTONUP, but it only seems to get called if I double click.    

If I add a handler for the NM_CLICK message, it does get called, but the arguments to that method do not provide either the mouse position or the index of the selected list item, so I don't know how to use it.

Any Ideas??     Thanks!
Question by:fisham29

Accepted Solution

Answers2000 earned 50 total points
ID: 1322488
Inside the handler for NM_CLICK, use GetNextItem to find the selection

int  nItem = GetListCtrl().GetNextItem( -1, LVNI_SELECTED ) ;

nItem then contains -1 if nothing is selected, or the index of the selected item


Expert Comment

ID: 1322489
Alternatively, you can do it in the WM_LBUTTONDOWN message like this:

void CMyListView::OnLButtonDown(UINT nFlags, CPoint point)
    // Determine who was clicked on
    int iTouched = GetListCtrl().HitTest(point);


Expert Comment

ID: 1322490
The answer from Answers2000 may not work if your list is a multiple-seclection listbox.

The NM_CLICK notification should give you enough information if you have version 4.71 or higher of the common controls.

Here is an excerpt from the doc for NMLISTVIEW:

typedef struct tagNMLISTVIEW{
    NMHDR   hdr;
    int     iItem;
    int     iSubItem;
    UINT    uNewState;
    UINT    uOldState;
    UINT    uChanged;
    POINT   ptAction;
    LPARAM  lParam;

As you can see you have everything in there, item, subitem, oldstate, etc....

To get the mouse position you can use:


It'll give it to you in screen coordinates, you can translate them with ::ScreenToClient if needed.
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Author Comment

ID: 1322491
The first answer did seem to work for me, but thanks for the additional comments...

I was avoiding use of the WM_LBUTTONDOWN message in case the user held down the button while he dragged around in the list view...I just wasn't sure the results would be intuitive.   I thought it made more sense to update on WM_LBUTTONUP, but for some reason I didn't get that message on a single click.

My list view is a single selection view (I should have mentioned that - sorry).    I was avoiding GetCursorPos because I thought it possible that the mouse would have moved before my handler was called and I could get weird results - maybe the time between the two events is so short it wouldn't matter....

Expert Comment

ID: 1322492
You could also have used:

MSG& msg = AfxGetThreadState()->m_lastSentMsg;

This gives you a way to check the last message, which would have to be the one you're handling. Check below the doc for the MSG structure, you have the POINT where the message is from.

Much safer than GetCursorPos I think.

MSG Structure
The MSG structure has the following form:

typedef struct tagMSG {     // msg  
    HWND   hwnd;      
    UINT   message;
    WPARAM wParam;
    LPARAM lParam;
    DWORD  time;
    POINT  pt;
} MSG;


Expert Comment

ID: 1322493
>> The answer from Answers2000 may not work if your list is a multiple-seclection listbox. [you mean List Ctrl/View]

Well it does if you loop thru, call GetNextItem again with the index of the previously found item to get the 2nd item, etc.

Expert Comment

ID: 1322494
>> Well it does if you loop thru, call GetNextItem again with the index of the previously found item to get the 2nd item, etc.
But then you don't really know which item was selected by the click.  That's what I meant by my comment on your answer.

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

23 Experts available now in Live!

Get 1:1 Help Now