Re: How can I tell when a checkbox is clicked on a List Control?
- From: Pucca <Pucca@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 18:44:01 -0800
Hi Martin, Thanks again for the help even though it's not a COM question any
more.
I do have just one last question about the listview item being selected or
not.
I thought the selection, checkbox thing was working perfectly until I just
found a bug. If the current item is selected but checkbox not checked, I
then checked the checkbox and my code failed to recgnize that. I used the
following to test out. Am I not doing it correctly? I'm listing out the few
lines of code first but they're part of the method "ValidateChanges(HWND
hwndDlg,NM_LISTVIEW FAR *lpnm)"
Doesn't this line, "if((retState & unitMask) == LVIS_SELECTED)", verfies if
the current listview item is selected?
UINT retState = ListView_GetItemState(hwndListView, index, unitMask);
else if ((lpnm->uOldState & LVIS_STATEIMAGEMASK) != (lpnm->uNewState &
LVIS_STATEIMAGEMASK))
{
if((lpnm->uOldState & 0x1000) && (lpnm->uNewState & 0x2000))
{
::MessageBox(NULL, L"This item is to be checked.", L"Unity -
ValidateChanges", MB_OK);
if(found)
userDataIter->symarkEnabled = true;
//If the item to be checked is also the currently seleted
//item then enable the DataGroup
if((retState & unitMask) == LVIS_SELECTED)
EnableDataGroup(TRUE);
return Success;
--------------------------------------------------------------------------------------------
bool CUserPage::ValidateChanges(HWND hwndDlg,NM_LISTVIEW FAR *lpnm)
{
LPWSTR lpwContext = new WCHAR[ONE_K];
LVHITTESTINFO lpvInfo;
UINT checkBoxChecked, unitMask=0;
bool found = false, Success = true, Fail = false;
int index = lpnm->iItem;
::MessageBox(NULL, L"We're at ValidateChanges", L"Unity - ValidateChanges",
MB_OK);
ListView_GetItemText(hwndListView, index, 0, lpwContext, ONE_K);
::MessageBox(NULL, lpwContext, L"lpwContext - ValidateChanges", MB_OK);
found = FindUserDataObject(lpwContext);
UINT retState = ListView_GetItemState(hwndListView, index, unitMask);
SendMessage(hwndListView, LVM_HITTEST, 0, (LPARAM)&lpvInfo);
checkBoxChecked = ListView_GetCheckState(hwndListView, index);
//if the item got checkBoxChecked then find the dataobject and enable it
//and then enable the userdata group area
//This item is to be selected/checked
//if(lpnm->uNewState == LVIS_SELECTED )
if (lpnm->uChanged & LVIF_STATE)
{
// state is about to change, like got/lost focus or selection changes etc.
if ((lpnm->uOldState & LVIS_SELECTED) != 0 &&
(lpnm->uNewState & LVIS_SELECTED ) == 0)
{
// item is going to be deselected
::MessageBox(NULL, L"This item is to be de-selected.", L"Unity -
ValidateChanges", MB_OK);
//Validate and save the userData
if (!found)
NewUserData = true;
if(SaveSyUserData(hwndDialog, lpwContext, index))
return Success;
else
return Fail;
//TRACE("ITEM DESELECTED: %d\n", index);
return TRUE; // allow change
}
else if ((lpnm->uOldState & LVIS_SELECTED) == 0 &&
(lpnm->uNewState & LVIS_SELECTED ) != 0)
{
// item is going to be selected
::MessageBox(NULL, L"Item Text label was clicked.", L"Unity -
ValidateChanges", MB_OK);
//Keep track of what's currently displayed
curIndex = index;
curContext = lpwContext;
//if(unitMask & LVIS_SELECTED)//It's to be selected
//{
::MessageBox(NULL, L"This item is to be selected.", L"Unity -
ValidateChanges", MB_OK);
_bstr_t bstrtContext = lpwContext;
SetGroupCaption(bstrtContext);
if(found)//The userData already exists
{
//display the corresponding user data, if available
NewUserData = false;
CopyToCurUserData();
DisplayUserData(&curUserData);
return Success;
}
else//new userdata, flush out the screen and create new object
{
NewUserData = true;
FlushOut(lpwContext, lpnm->iItem);
if(checkBoxChecked)
EnableDataGroup(TRUE);
else
EnableDataGroup(FALSE);
return Success;
//CreateNewUserDataObj(lpwContext);
}
//TRACE("ITEM SELECTED: %d\n", index);
return true; // allow change
}
else if ((lpnm->uOldState & LVIS_STATEIMAGEMASK) != (lpnm->uNewState &
LVIS_STATEIMAGEMASK))
{
if((lpnm->uOldState & 0x1000) && (lpnm->uNewState & 0x2000))
{
::MessageBox(NULL, L"This item is to be checked.", L"Unity -
ValidateChanges", MB_OK);
if(found)
userDataIter->symarkEnabled = true;
//If the item to be checked is also the currently seleted
//item then enable the DataGroup
if((retState & unitMask) == LVIS_SELECTED)
EnableDataGroup(TRUE);
return Success;
// image (checkbox) is about to change
//TRACE("IMAGE CHANGING: %d %x -> %x\n", index,lpnm->uOldState,
lpnm->uNewState);
// uOldState=0x1000 -> uNewState=0x2000: item is going to bechecked
// uOldState=0x2000 -> uNewState=0x1000: item is going to beunchecked
//return FALSE; // prohibit change
}
else if((lpnm->uOldState & 0x2000) && (lpnm->uNewState & 0x1000))
{
::MessageBox(NULL, L"This item is to be un-checked.", L"Unity -
ValidateChanges", MB_OK);
if(found)
userDataIter->symarkEnabled = false;
//If the item to be un-checked and is also the currently seleted
//item then disable the DataGroup
if((retState & unitMask) == LVIS_SELECTED)
EnableDataGroup(FALSE);
return Success;
}
}//end checkbox/selection changing
}//end if (lpnm->uChanged & LVIF_STATE)
return true; // allow change
}
--
Thanks.
"xrxst32" wrote:
hi pucca,.
you may attach your userdata when inserting your items with
ListView_InsertItem(), just set within the LVITEM structure the lParam
member to some value (don't forget to set the LVIF_PARAM flag in the
mask) or set it later with the function ListView_SetItem() (you too
have to fill out a LVITEM structure, so it's just the same as before).
to get your assigned data back, use the ListView_GetItem() function
(set the LVIF_PARAM flag in the mask member) and the listview will
return your data (e.g. a pointer to the corrosponding object) within
the lParam member).
see the MSDN for more information.
btw. this is not really a COM related topic anymore.
- Follow-Ups:
- Prev by Date: ADsGetOject crashing strangely.
- Next by Date: Broken Control Registration
- Previous by thread: ADsGetOject crashing strangely.
- Next by thread: Re: How can I tell when a checkbox is clicked on a List Control?
- Index(es):
Relevant Pages
|