Click here to Skip to main content
Licence CPOL
First Posted 28 Dec 2003
Views 78,291
Downloads 2,468
Bookmarked 38 times

CCheckComboBox II

By PJ Arends | 4 Jul 2004
A ComboBox that has a checkbox by each of its items.

1

2
3 votes, 33.3%
3
3 votes, 33.3%
4
3 votes, 33.3%
5
3.88/5 - 9 votes
μ 3.88, σa 1.52 [?]

Sample Image - CheckComboBox.png

Introduction

This control is actually a modification of the CCheckComboBox class that was written by Magnus Egelberg, Lunalogik. Magnus' control had just about everything I was looking for in a CheckComboBox except one small feature. It did not allow you to check an item in the static text portion of the combo box, you had to drop down the list box in order to make the selections. All the drawing code in this class was written by Magnus. The only additions I made was the code to handle the check box in the static text area, and I removed the dependency on the item data for storing the check state of the items. It is now stored in a CByteArray member variable.

Usage

To use the control, you have to include the CheckComboBox.h header file and add the CheckComboBox.cpp file to your project.

If the control is used on a dialog, it has to have the CBS_DROPDOWNLIST and CBS_OWNERDRAWVARIABLE combo box styles.

Command Notifications

In addition to the regular CBN_* notifications that are generated by the combobox, the CCheckComboBox will also generate a BN_CLICKED notification whenever a user changes the check state of a check box. You can handle the BN_CLICKED notification exactly the same way you would handle it if it was generated by a button control. Use the GetCurSel() member function to get the index of the checkbox that generated the BN_CLICKED notification.

BEGIN_MESSAGE_MAP(CCCheckCombo_demoDlg, CDialog)
    // use the ON_BN_CLICKED macro to catch the BN_CLICKED
    // command that is generated by the CCheckComboBox
    ON_BN_CLICKED(IDC_COMBO1, OnCheckBox)
END_MESSAGE_MAP()
 
...
 
void CCCheckCombo_demoDlg::OnCheckBox()
{
    int sel = m_CheckCombo.GetCurSel();
    CString text;
    m_CheckCombo.GetLBText(sel, text);
    BOOL checked = m_CheckCombo.GetCheck(sel);
    TRACE(_T("Item %d (\"%s\") was %s \n"), sel, 
       text, checked ? _T("checked") : _T("unchecked"));
}

Member Functions

These are the public member functions added to the CCheckComboBox. All CComboBox member functions can also be used.

  • BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)

    Use this function to dynamically create a CCheckComboBox control. The required combo box styles are set automatically.

    Return value

    Nonzero if successful, otherwise 0.

    Parameters

    dwStyle The WS_* and CBS_* styles for the control. The CBS_DROPDOWNLIST and CBS_OWNERDRAWVARIABLE styles are set automatically if they are not specified.
    rect The position and size of the CCheckComboBox.
    pParentWnd Specifies the CCheckComboBox’s parent window (usually a CDialog). It must not be NULL.
    nID Specifies the CCheckComboBox’s control ID.
  • BOOL GetCheck(int nIndex)

    Use this function to get the checked state of the check box at the specified zero based index.

    Return value

    TRUE if the check box is checked, otherwise FALSE.

    Parameters

    nIndex The zero based index of the check box
  • BOOL SetCheck(int nIndex, BOOL bCheck = TRUE)

    Sets the check state for the check box at the specified zero based index.

    Return value

    The previous check state. TRUE if the check box was checked, FALSE if not.

    Parameters

    nIndex The zero based index of the check box
    bCheck The new check state. TRUE to check the check box, FALSE to uncheck it.
  • void CheckAll(BOOL bCheck = TRUE)

    Sets the checked state of all the check boxes in the CCheckComboBox control.

    Return value

    There is no return value.

    Parameters

    bCheck The new check state. TRUE to check the check boxes, FALSE to uncheck them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

PJ Arends



Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSome features ! PinmemberBui Tan Duoc17:02 21 Mar '11  
QuestionHow can I use this control on a dialog bar ? Pinmembermesajflaviu9:01 11 Jan '11  
GeneralI create a VC2008 project, failed to used your code, when addstring function is invoked, software crashed Pinmemberskysailor_x4:02 19 Apr '10  
QuestionWhy SELCHANGE was called twice? Pinmemberquyun3:08 14 Feb '10  
QuestionDoesn't work on 64-bit Vista Pinmembermpietarin1:33 14 Aug '08  
Hi,
 
I have used this CCheckComboBox-control in my project. It works fine in XP (32-bit and 64-bit). It works also on 32-bit Vista. For some reason it doesn't work on 64-bit Vista. I can't check or uncheck any checkbox in dropdown list. I can create list and dropdown system also works but (un)checking just doesn't work.
 
I debugged this so far that class seems to do the subclassing of listabox i.e. it seems to register the message handler function (ComboBoxListBoxProc-function on CCheckComboBox::OnCtlColorListBox -method). But there won't be any messages (WM_CHAR or WM_LBUTTONDOWN) to handle (as it happens on working platforms when I click listbox and do the checking/unchecking).
 
I had also fix the code that it would compile on 64-bit platform:
 
LRESULT CCheckComboBox::OnCtlColorListBox(WPARAM wParam, LPARAM lParam) 
{
	// If the listbox hasn't been subclassed yet, do so...
	if (m_hListBox == 0) {
		HWND hWnd = (HWND)lParam;
 
		if (hWnd != 0 && hWnd != m_hWnd) {
			// Save the listbox handle
			m_hListBox = hWnd;
 
			// Do the subclassing
#ifdef _WIN64
			m_pWndProc = (WNDPROC)GetWindowLong(m_hListBox, GWLP_WNDPROC);
			SetWindowLong(m_hListBox, GWLP_WNDPROC, (LONG)ComboBoxListBoxProc);
#else
			m_pWndProc = (WNDPROC)GetWindowLong(m_hListBox, GWL_WNDPROC);
			SetWindowLong(m_hListBox, GWL_WNDPROC, (LONG)ComboBoxListBoxProc);
#endif
		}
	}
 
	
	return DefWindowProc(WM_CTLCOLORLISTBOX, wParam, lParam);
}
 

I use currently MS Visual C++ 2008. I have had CCheckComboBox-control working also with VC++ 2003 and 2005 versions on XP 32-bit.
 
Any help would be preciated...
AnswerRe: Doesn't work on 64-bit Vista Pinmembermpietarin6:30 18 Aug '08  
QuestionUsing this control in ATL PinmemberRajkumar Rachoti3:10 9 Nov '06  
Questionc# code Pinmembermrinal1214:06 27 Oct '06  
AnswerRe: c# code PinmemberPJ Arends18:27 27 Oct '06  
GeneralDisabling item selection from dropdown list box PinmemberDaalu23:38 20 Sep '06  
GeneralRe: Disabling item selection from dropdown list box PinmemberPJ Arends9:19 21 Sep '06  
GeneralUsage question... Pinmembersoonaz6:26 30 Aug '06  
GeneralRe: Usage question... PinmemberPJ Arends7:07 30 Aug '06  
Generalgood... but... PinmemberMin-Seok Kim2:53 26 Jul '05  
GeneralComment Pinmembermcbain2:00 30 Dec '03  
GeneralRe: Comment PinmemberPJ Arends8:04 30 Dec '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 5 Jul 2004
Article Copyright 2003 by PJ Arends
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid