views:

817

answers:

3

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?

A: 
BEGIN_MESSAGE_MAP(cDlgRun, CDialog)

    ON_NOTIFY(NM_CLICK, IDC_LIST2, &cDlgRun::OnNMClickList2)

END_MESSAGE_MAP()

/**

  User has selected a row

*/
void cDlgRun::OnNMClickList2(NMHDR *pNMHDR, LRESULT *pResult)
{

    int row_number = myList2.GetSelectionMark();
    if( row_number < 0 )
        return;
ravenspoint
+3  A: 

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 & LVNI_SELECTED))
    {
        // do stuff...
    }
}
djeidot
+2  A: 

djeidot is right on.

I just want to add that there is no OnSelectionChanged() because the ListView supports multi-selection (although this can be disabled). Therefore, a single-selection listview will send you two events: Old item unselected AND New item selected.

Serge - appTranslator