C++/VB - Detect changes in ClistCtrl control with checkbox

Asked By asoni1
13-Jan-09 11:31 AM
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.
Visual Studio
(1)
OnNMClickList
(1)
PNMHDR
(1)
CList.GetSelectedCount
(1)
CList.GetItemState
(1)
CSpanRestoreDlg
(1)
PNMListView
(1)
PNMLV
(1)
  Ajay Kalra replied...
14-Jan-09 03:04 AM
On Jan 13, 11:31=A0am, asoni12 <ason...@>

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

--
Ajay
  asoni1 replied...
13-Jan-09 01:38 PM
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 replied...
13-Jan-09 02:29 PM
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 replied...
13-Jan-09 04:49 PM
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
  asoni1 replied...
14-Jan-09 02:54 AM
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 replied...
28-Feb-09 04:06 PM
http://msdn.microsoft.com/en-us/library/bb761250.aspx

You can look this link.
Create New Account
help
with an applications consisting of mixed native and managed code writted in C++ / CLI in Visual Studio 2005. The application runs fine in "Debug mode", however when I compile the application in around a native class. I can also see the following in the output window of Visual Studio 2008: '3DWorkbench.exe' (Managed): Loaded 'c: \ svn \ auto3d \ 3dworkbench \ release \ GcpsDotNetApi.dll', Symbols loaded. First Measurement Systems and Data Analysis SINTEF ICT [http: / / www.sintef.com / omd] VC Language Discussions Visual Studio 2008 (1) Visual Studio 2005 (1) DllMainCRTStartup (1) RtlpNtMakeTemporaryKey (1) RtlInitializeSListHead (1) CrtIsValidHeapPointer (1) VB (1) GcpsDotNetApi (1) When
mfc project not compile in visual studio 2008 C++ / VB I made a mfc project using wizard in visual studio 2008 and it didn`t compile, . i didn`t add anything, just made it as didn`t help. For more informations i have win xp sp2 and freshly installed microsoft visual studio 2008 in a default way. errors: 1> stdafx.cpp 1> c: \ program files \ microsoft visual studio 9.0 \ vc \ atlmfc \ include \ winnt.h(3019) : error C2146: syntax error : missing ';' before identifier
my own string.h C++ / VB Hi, I'm having a weird problem when using Visual Studio. I'm creating a small library with different utility classes. Thing is that everything compiles far more errors (please note that 100+ errors are displayed) * ** 1> c: \ program files \ microsoft visual studio 8 \ vc \ include \ cstring(18) : error C2039: 'memchr' : is not a member of '`global namespace'' 1> c: \ program files \ microsoft visual studio 8 \ vc \ include \ cstring(18) : error C2873: 'memchr' : symbol cannot be used in a using declaration 1> c: \ program files \ microsoft visual studio 8 \ vc \ include \ cstring(18) : error C2039: 'memcmp' : is not a member of '`global namespace
SSH Library for SFTP implementation in VC++ with Visual Studio 200 C++ / VB Does Microsoft provide any SSH Library for SFTP Connection implementation in VC++ using Visual Studio 2008? I work on Visual Studio Extension Package created for Visual Studio 2003 & Visual Studio 2005, which is now migrated to Visual Studio 2008. Our package adds