[Webinar] Learn how to a build a cloud-first strategyRegister Now


SingleClick Selection in CListCtrl

Posted on 1998-09-23
Medium Priority
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 200 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.
Technology Partners: 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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

865 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