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.
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 60
Pressing cancel button with C# UI Automation 3 69
WinWaitActive parameters 12 31
Problem to open Excel file 15 133
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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