Click here to Skip to main content
13,251,998 members (66,828 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


34 bookmarked
Posted 31 Jan 2009

CStackableGroupBox - Class to Hide Group of Controls and Shrink Dialog Window

, 31 Jan 2009
Rate this:
Please Sign up or sign in to vote.
CStackableGroupBox allows you to create multiple groups of controls and then show or hide them at runtime


Figure 1: Dialog as it appears in Resource Editor. The same dialog holds properties for line, rectangle and circle objects.


Figure 2: Two run-time versions of the same dialog showing controls only relevant for circle object. The second dialog has "Fill" box checked and expanded at run-time.


CStackableGroupBox allows you to create a single dialog resource and a single dialog class that hold several groups of controls and selectively show those groups at run time. Hiding a group automatically shrinks the dialog and brings up all the controls below it. Group can also include a checkbox control that always remains visible.


Sometimes you need to write a dialog for objects that are different but share some of the same properties. The easiest approach is to write a series of similar dialogs using copy/paste, but maintaining and expanding them is a big headache. You can also stick all the controls in one dialog and disable the ones that don't apply. Another alternative is to organize data in groups and only show applicable groups for each object. That's where CStackableGroupBox comes in handy.

It is also applicable in a situation where you are writing a dialog with a group of advanced controls that need not be shown to the user by default to avoid overwhelming him/her. But when clicked on "Advanced" checkbox or button, the advanced controls should show. See "Fill" group in Figure 2 for an example.

Using the Code

  1. Include StackableGroupBox.h and  StackableGroupBox.cpp files in your project.
  2. In resource editor, organize controls that will be toggled at runtime into groups. Give each groupbox control unique ID (i.e. not IDC_STATIC).
  3. In the header class for your dialog, add CStackableGroupBox instances for each group.
  4. In the OnInitDialog, call init_group with IDs of the associated checkbox (0 if none) and groupbox control ID. 
  5. In OnInitDialog, call show_group_expand_dialog to set initial visibility state.
  6. Call show_group_expand_dialog  anytime a specific group needs to be shown or hidden.

Here's the code from the demo provided (I used a modified version of UndoDemo from my UndoHistoryPopup article):

// CDlg_AddObject Header file 

class CDlg_AddObject : public CDialog
// ... snipped ...
	CStackableGroupBox m_group_line;
	CStackableGroupBox m_group_circle;
	CStackableGroupBox m_group_rect;
	CStackableGroupBox m_group_fill;
// ... snipped ...
// CDlg_AddObject CPP file 

BOOL CDlg_AddObject::OnInitDialog() 
/// .... snipped....

	m_group_circle.init_group(this, 0, IDC_GROUP_CIRCLE);
	m_group_rect.init_group(this, 0, IDC_GROUP_RECTANGLE);
	m_group_line.init_group(this, 0, IDC_GROUP_LINE);
	m_group_fill.init_group(this, IDC_CHECK_FILL, IDC_GROUP_COLOR);

	m_group_circle.show_group_expand_dialog(obj.type == DOT_ELLIPSE, this);
	m_group_rect.show_group_expand_dialog(obj.type == DOT_RECTANGLE, this);
	m_group_line.show_group_expand_dialog(true, this);  //<-- show always
			(obj.type != DOT_LINE && obj.do_fill, this);

	GetDlgItem(IDC_CHECK_FILL)->EnableWindow(obj.type != DOT_LINE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE

void CDlg_AddObject::OnCheckFill()
	obj.do_fill = (BST_CHECKED == IsDlgButtonChecked(IDC_CHECK_FILL));
			(obj.type != DOT_LINE && obj.do_fill, this);

How It Works

When you call init_group, all the controls inside the groupbox are saved in the array. Another array stores all dialog controls below the group. This should be done during the dialog's initialization process to correctly position controls later.

Calling show_group_expand_dialog hides controls of that group, then cycles through the stored array of controls below the group and moves them all up in the newly opened space. The dialog window is then contracted by the same height difference.


  • Controls placed to the right or left side of the bottom-most group will not be shifted up when the group is hidden. You will need to do it manually.
  • Controls added or moved in and out of the groupbox area after you call init_group will not be properly positioned later.
  • You can set only one control to always stay visible. So if at the header of your group there's more than one control, you need either modify this class or move them above the groupbox.


  • 2007-05-16 - Created the class
  • 2009-02-01 - Renamed to CStackableGroupBox and released for CodeProject 


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


About the Author

Damir Valiulin
Software Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralI have the similar control Pin
M A V25-May-09 11:33
memberM A V25-May-09 11:33 
GeneralRe: I have the similar control Pin
Damir Valiulin26-May-09 7:55
memberDamir Valiulin26-May-09 7:55 
GeneralJust what I needed Pin
Christopher Latham12-Feb-09 14:50
memberChristopher Latham12-Feb-09 14:50 
Generalvery useful Pin
only_jack1-Feb-09 0:17
memberonly_jack1-Feb-09 0:17 
GeneralRe: very useful Pin
Damir Valiulin1-Feb-09 8:34
memberDamir Valiulin1-Feb-09 8:34 
GeneralRe: very useful Pin
zzh122152-Feb-09 21:28
memberzzh122152-Feb-09 21:28 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 1 Feb 2009
Article Copyright 2009 by Damir Valiulin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid