Click here to Skip to main content
15,903,201 members
Articles / Desktop Programming / MFC
Article

CGroupCheckBox control

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
16 May 20042 min read 48.4K   2.3K   25   3
A fully automated groupbox and checkbox for controling the state of its surrounded controls

Introduction

I decided I needed a combination Groupbox and Checkbox control. I wanted all of the controls that lie inside the groupbox to become enabled/disabled in response to a click of a checkbox that is associated with the groupbox.

Background

I jumped right in and started writing one. When I bogged down I went looking to see if anyone else had tried this. After examining Ming Liu's code from his article "CGroupCheck - Checkbox associated with a groupbox" I solved my problems. I owe him a debt of gratitude for paving the way for me, although I took a different approach to the problem.

Using the code

Simply add a groupbox to your dialog, and give it an ID (change the default id of IDC_STATIC to something else). Put all of the controls you want it to enable/disable inside the groupbox. All of the control's client areas must lie completely inside the groupbox. Create a member variable of type CButton using class wizard. Change the CButton to CGroupCheckBox in the header file.

DDX_

CGroupCheckBox also defines a custom DDX_ function,

void 
AFXAPI DDX_GroupCheck(CDataExchange* pDX, int nIDC, int& value)
, that you can add to the dialogs DoDataExchange() function. Simply create a public BOOL variable such as m_bIgnore and do as done below:

void CGroupCheckBoxDemoDlg::DoDataExchange(CDataExchange* pDX)

{
    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CGroupCheckBoxDemoDlg)
    DDX_Control(pDX, IDC_GROUP2, m_ctlIgnore);
    //}}AFX_DATA_MAP



    DDX_GroupCheck(pDX, IDC_GROUP2, m_bIgnore);

}

Styles

Notice the above dialog contains two styles of CGroupCheckBox. The one labeled Touch All enables/disables all of its contained controls and is created by default. The other, labeled Ignore Static IDs, enables/disables only those controls whose IDs are not IDC_STATIC. This style is set with a call to the member function SetStyle().

SetStyle() is the only call you need to make, and then it is only needed to change from the default style. Example of usage from the above app:

BOOL CGroupCheckBoxDemoDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Change the CGroupCheckBox style from the default.
     m_ctlIgnore.SetStyle(CGroupCheckBox::TCH_IGNORE);

    return TRUE;
}

Points of Interest

OGX file

I find that the easiest way to reuse my classes is to add them to the Component Gallery. Those classes are then available to you via Class Wizard. In the case of CGroupCheckBox, you can create a member variable of this type with class wizard. The demo program also uses my CGlyphButton that I wrote another article on previously. It is also available as an OGX file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralControls must only intersect Pin
Bjorn Tesselaar5-Jul-04 3:32
Bjorn Tesselaar5-Jul-04 3:32 
Questionwhat's in a name? Pin
Bruce Smotherman18-May-04 2:13
Bruce Smotherman18-May-04 2:13 
GeneralPlease post full source file Pin
CodeHead17-May-04 2:22
CodeHead17-May-04 2:22 

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

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