1

I've been playing with the list view and came across this post: How to detect a CListCtrl selection change?

However the code used there has a major flow, it doesn't work with multiple selection (as pointed out in that thread). So my question is how can I make the code work with multiselection (eg. selection with shift or ctrl)?

Community
  • 1
  • 1
user555
  • 1,564
  • 2
  • 15
  • 29
  • Does this not work: http://stackoverflow.com/questions/464675/clistctrl-how-to-enable-multiple-selection there is a comment there about AlwaysShowSelection Property in the comments to the accepted answer – EdChum Apr 30 '12 at 18:22
  • AlwaysShowSelection didn't work. It only kept the selection when the list view was out of focus. The main problem is that the function will not catch a change when you select multiple rows with shift, and then select one of them (which deselects the other ones). – user555 Apr 30 '12 at 18:32
  • If that is the only situation it doesn't detect, then can't you simply catch a button click, and check the selection in order to fix the behavior? –  Apr 30 '12 at 19:15
  • `LVN_ITEMCHANGED` notification gets you all notifications, including for multiply selected items. Code snippet there does possibly wrong `if` to get into "do stuff". – Roman R. May 01 '12 at 09:30

2 Answers2

0

I've written a handy function to see if your OnItemChanged notification was due to a selection change:

BOOL IsItemSelChanged(NMLISTVIEW* pNMListView)
{
    // call this from your OnItemchangedMyListCtrl function in your dialog class

    if(!(pNMListView->uChanged & LVIF_STATE))
    {
        return(FALSE);
    }

    if((pNMListView->uOldState & LVIS_SELECTED) == (pNMListView->uNewState & LVIS_SELECTED))
    {
        return(FALSE);
    }

    return(TRUE);
}
0

Handle LVN_ITEMCHANGED and LVN_ODSTATECHANGED, all you needed...

If a list-view control has the LVS_OWNERDATA style, and the user selects a range of items by holding down the SHIFT key and clicking the mouse, LVN_ITEMCHANGED notifications are not sent for each selected or deselected item. Instead, you will receive a single LVN_ODSTATECHANGED notification, indicating that a range of items has changed state.

SevenWow
  • 305
  • 3
  • 14