How to detect a CListCtrl selection change?

rec picture rec · Sep 23, 2009 · Viewed 36.9k times · Source

I want to execute some code when the user selects a row in a CListCtrl (report view, I don't care about the other viewing modes).

How do I catch this event? is there some message I can map or a method like "OnSelectionChanged" or something like that?

Answer

djeidot picture djeidot · Sep 23, 2009

Also try:

BEGIN_MESSAGE_MAP(cDlgRun, CDialog)
    ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST2, OnItemchangedList2)
END_MESSAGE_MAP()

/* ... */

void cDlgRun::OnItemchangedList2(NMHDR* pNMHDR, LRESULT* pResult) 
{
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    if ((pNMListView->uChanged & LVIF_STATE) 
        && (pNMListView->uNewState & LVIS_SELECTED))
    {
        // do stuff...
    }
}