Re: How can I tell when a checkbox is clicked on a List Control?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.


.



Relevant Pages

  • Re: How can I tell when a checkbox is clicked on a List Control?
    ... different selection or a checkedbox status is changed. ... selected I would get 4,5 notification of that checkbox was clicked and the ... return Success; ...
    (microsoft.public.win32.programmer.ole)
  • Re: Berrys paradox, tautology and evolution naming conventions
    ... "The real problem with Darwin's selection theory, however, is that it ... "Reproductive success" was a term crafted to reflect the battle theme ... Normal people understand common terms by the context they find ... really being spun into biology: dominate, success, 'red in tooth and ...
    (talk.origins)
  • Re: Natural selection as a non-random selection process - Wikipedia
    ... The genetic drift article had the non-random part added to NS on 8 Dec. ... from the other significant evolutionary mechanism, natural selection, ... directed, who did the directing? ... Calling this a "success" is wrong because if another ...
    (talk.origins)
  • Re: Writing from the POV of older people than ones self
    ... tendency for individuals *within* the group to fight over scarce ... the success of your group - a genetic trait that caused people to ... behavior that cost you 1 unit of reproductive success but gave ... We would expect group selection to be weak relative to individual ...
    (rec.arts.sf.composition)
  • Re: checkboxs
    ... "Dave Peterson" wrote: ... I have multiple checkboxs within one cell per worksheet. ... Say there can be on selection from the rows of T, ... checked somehow to see how many of the same named checkbox have been checked. ...
    (microsoft.public.excel.misc)