Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect changes in ClistCtrl control with checkbox

3,306 views
Skip to first unread message

asoni12

unread,
Jan 13, 2009, 11:31:01 AM1/13/09
to
I am using a CListCtrl with LVS_EX_CHECKBOXES extended style. How can I
detect that a previously checked item has been unchecked or a previously
unchecked item has been checked. I know that LVN_ITEMCHANGED event is
triggered whenever its state gets changed but how to find if the item has
been checked / unchecked and its previously state.
Any help is appreciated.

Ajay Kalra

unread,
Jan 13, 2009, 11:51:07 AM1/13/09
to
On Jan 13, 11:31 am, asoni12 <ason...@discussions.microsoft.com>
wrote:

Take a look at ListView_GetCheckState: http://msdn.microsoft.com/en-us/library/bb761250(VS.85).aspx
and LVN_ITEMCHANGING: http://msdn.microsoft.com/en-us/library/bb774847(VS.85).aspx

--
Ajay

asoni12

unread,
Jan 13, 2009, 1:38:04 PM1/13/09
to

"Ajay Kalra" wrote:

Hi Ajay,
Thanks for the quick reply.
We get a pointer to an NMLISTVIEW structure in case of both both
LVN_ITEMCHANGING and LVN_ITEMCHANGED event handlers but with this structure
how can we make sure whether items’s checked/unchecked state has been
toggled or something else has changed?
I mean is there any bit in this structure member which is set/unset when the
state gets toggled?

Ian Semmel

unread,
Jan 13, 2009, 2:29:00 PM1/13/09
to
In the LVN_ITEMCHANGED handler you can put

int ns = pNMListView ->uNewState & LVIS_STATEIMAGEMASK;

if ( ( ns & 0x2000 ) != 0 )
// Checkbox set
else if ( ( ns & 0x1000 ) != 0 )
// Checkbox unset
else
// Something else happened

Tom Serface

unread,
Jan 13, 2009, 4:49:49 PM1/13/09
to
ON_NOTIFY(NM_CLICK, IDC_LIST, &CSpanRestoreDlg::OnNMClickList)

// Check or uncheck item. If more than one item is selected then use the
one from the
// hit test to determine how we are setting the others (I.E., if it is off
we turn the other
// selected ones on).
void CMyDlg::OnNMClickList(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
LVHITTESTINFO hitinfo;
*pResult = 0;

bool bChecked = false;

//Copy click point
hitinfo.pt = pNMListView->ptAction;

//Make the hit test...
int nItem = m_cList.HitTest(&hitinfo);

if(hitinfo.flags != LVHT_ONITEMSTATEICON)
return; // Didn't click on an icon

if(m_cList.GetItemState(nItem,LVIS_SELECTED) != LVIS_SELECTED) {
// They clicked on one that is not selected... just change it
// ... do something here
bChecked = m_cList.GetCheck(nItem);
bChecked = !bChecked;
m_cList.SetCheck(nItem,bChecked);
*pResult = 1;
return;
}

// Get the checked state from the one they clicked on, but change all
the ones that are selected
UINT uSelectedCount = m_cList.GetSelectedCount();
// Update all of the selected items.
if (uSelectedCount > 0) {
nItem = -1;
m_cList.SetRedraw(false);
for (UINT i=0;i < uSelectedCount;i++) {
nItem = m_cList.GetNextItem(nItem, LVNI_SELECTED);
m_cList.SetCheck(nItem,bChecked);
}
*pResult = 1;
m_cList.Invalidate();
m_cList.SetRedraw();
}
}

Tom

"asoni12" <aso...@discussions.microsoft.com> wrote in message
news:C3A5D065-15DA-46BB...@microsoft.com...

asoni12

unread,
Jan 14, 2009, 2:54:00 AM1/14/09
to
Thanks Ian. I did it this way and it works fine for me-
I just wanted to confirm, since I am using constant 0x2000 and 0x1000 here,
can I be sure that these checks will not be bricked in some other environment
like other version of windows OS or Visual Studio?

int oldState = -1;
int newState = -1;
int changedState= -1;

int ns1 = pNMLV->uOldState & LVIS_STATEIMAGEMASK;
int ns2 = pNMLV->uNewState & LVIS_STATEIMAGEMASK;

if ((ns1 & 0x2000) != 0)// find the previous state
{ //Checkbox set
oldState = 1;
}
else if ((ns1 & 0x1000) != 0)
{
//Checkbox unset
oldState = 0;
}

if (-1 != oldState)// If got the previous state then find the new state
{
if ((ns2 & 0x2000) != 0)
{ //Checkbox set
newState = 1;
}
else if ((ns2 & 0x1000) != 0 )
{
//Checkbox unset
newState = 0;
}
if ( (-1 != newState) && (oldState != newState))
{
changedState = newState - oldState;
if (1 == changedState)
{
AfxMessageBox(_T("Item has been checked"));
}
else if (-1 == changedState)
{
AfxMessageBox(_T("Item has been unchecked"));

Sanoop Das K

unread,
Feb 28, 2009, 1:37:11 AM2/28/09
to
0 new messages