Re: HELP! stopping selection in a CListCtrl
To deal with a multi-select CListCtrl, it is likely better to focus on the =
LVIS_FOCUSED state rather than LVIS_SELECTED state (pun intended). For val=
idation, you want to use the LVN_ITEMCHANGING event rather than the LVN_ITE=
MCHANGED event.
So the validation check is only done on the condition of losing focus:
// Process this message
if ((pNMLV->uOldState & LVIS_FOCUSED) && !(pNMLV->uNewState & LVIS_FOCUS=
ED))
{
// Validate
// ...
}
Further, when needing to validate and cancel the change to the new item, yo=
u'll need to not only explicitly revert the state but also set *pResult to =
non-zero so the code looks like:
if (bRevert == TRUE)
{
m_listCtrl.SetItemState(pNMLV->iItem, LVIS_SELECTED | LVIS_FOCUSED, L=
VIS_SELECTED | LVIS_FOCUSED);
*pResult = 1;
return;
}
All this being said...after returning here the LVN_ITEMCHANGING event is st=
ill received a second time! In this case, the pNMLV->iItem is the same val=
ue. Why is this happening?! Here is the results of outputting the state w=
hen the first element (i.e. iItem=0) is selected, validated, and prevente=
d from switching:
LVN_ITEMCHANGING, iItem=0, uChanged=8, uOldState=2, uNewState=0
LVN_ITEMCHANGED, iItem=0, uChanged=8, uOldState=2, uNewState=0
LVN_ITEMCHANGING, iItem=0, uChanged=8, uOldState=1, uNewState=0
<< Catch this pending state change here...prompt user...user cancels, CList=
Ctrl::SetItemState() is called setting LVIS_SELECTED | LVIS_FOCUSED, and *p=
Result set to 1.>>
LVN_ITEMCHANGING, iItem=0, uChanged=8, uOldState=1, uNewState=3
LVN_ITEMCHANGED, iItem=0, uChanged=8, uOldState=1, uNewState=3
LVN_ITEMCHANGING, iItem=0, uChanged=8, uOldState=1, uNewState=0
<< Why is this being hit a second time?!? >>
LVN_ITEMCHANGINGm, iItem=0, uChanged=8, uOldState=3, uNewState=3
Can anyone offer any insight?
David
PS: 13 years later...but still using this stuff! Bump!
PPS: Just found this nice little summary here, although it doesn't answer t=
his specific question may likely be useful for [novice] users of CListCtrl =
should they stumble on this thread:
http://www.celticwolf.com/useful-information/faqs/10-clistctrl-faq