Objective Grid: Toggling a checkbox

Article ID: 508
Last updated: 16 Apr, 2018
Article ID: 508
Last updated: 16 Apr, 2018
Revision: 3
Views: 32969
Posted: 09 Jan, 2001
by Meltreger B.
Updated: 16 Apr, 2018
by Meltreger B.

Problem


When using the CGXCheckListComboBox class, currently I have to click on the actual checkbox to toggle the value. How can I change the behavior so the checkbox is checked/unchecked whenever I click anywhere on the item?


Cause



Action


In order to do this, you should derive a class from CGXCheckListComboLBox (which itself derives from the MFC class CCheckListBox). In your subclass, you can process the WM_LBUTTONDOWN message as shown below. You will also have to subclass CGXCheckListComboBox class and override the CreateListBox member. There you should instantiate your CGXCheckListComboLBox derivative.

Example:

void CYourCheckListComboLBox::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bLButtonDown = TRUE;
m_nOldSel = GetCurSel();
SetFocus();

// determine where the click is
BOOL bInCheck;
int nIndex = CheckFromPoint(point, bInCheck);

// if the item is disabled, then eat the click
if (!IsEnabled(nIndex))

return;
if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
{
// toggle check mark automatically if check mark was hit
if (TRUE) //This was if(bInCheck) in the base class
{
CWnd* pParent = GetParent();
ASSERT_VALID(pParent);

int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;

int nCheck = GetCheck(nIndex);
nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
SetCheck(nIndex, (nCheck + 1) % nModulo);
InvalidateCheck(nIndex);
CListBox::OnLButtonDown(nFlags, point);

// Inform of check
pParent->SendMessage(WM_COMMAND,

MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
(LPARAM)m_hWnd);
return;
}
}

// do default listbox selection logic
CListBox::OnLButtonDown(nFlags, point);

}

This article was:   Helpful | Not helpful
Report an issue
Article ID: 508
Last updated: 16 Apr, 2018
Revision: 3
Views: 32969
Posted: 09 Jan, 2001 by Meltreger B.
Updated: 16 Apr, 2018 by Meltreger B.

Others in this category