[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register 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
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

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.
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

656 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