• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

SingleClick Selection in CListCtrl

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!
0
fisham29
Asked:
fisham29
1 Solution
 
Answers2000Commented:
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


0
 
jstolanCommented:
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);
    .
    .
    .

0
 
plarocheCommented:
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;
} NMLISTVIEW, FAR *LPNMLISTVIEW;

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

To get the mouse position you can use:

::GetCursorPos

It'll give it to you in screen coordinates, you can translate them with ::ScreenToClient if needed.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
fisham29Author Commented:
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....
0
 
plarocheCommented:
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;

0
 
Answers2000Commented:
>> 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.
0
 
plarocheCommented:
>> 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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now