Programming Tips - MFC: Notified when selection changes in a List Control (not listbox)

Date: 2019nov1 Platform: MFC OS: Windows Language: C/C++ Keywords: CListCtrl Q. MFC: Notified when selection changes in a List Control (not listbox) A. In Visual Studio you can add event LVN_ITEMCHANGED which adds this code to your project: In the .h
afx_msg void OnLvnItemchangedMyList(NMHDR *pNMHDR, LRESULT *pResult);
In the .cpp
BEGIN_MESSAGE_MAP(CMyDialogDlg, CDialog) ON_NOTIFY(LVN_ITEMCHANGED, IDC_MYLIST, &CMyDialogDlg::OnLvnItemchangedMyList) END_MESSAGE_MAP() void CMyDialogDlg::OnLvnItemchangedMyList(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); *pResult = 0; // For single select, you can see what is selected like this: const int iSel = GetNextItem(-1, LVNI_SELECTED); const BOOL somethingIsSelected = iSel >= 0; }