Click here to Skip to main content
15,884,986 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
4.05/5 (14 votes)
31 Jan 2009CPOL3 min read 40.6K   1.2K   34   6
CStackableGroupBox allows you to create multiple groups of controls and then show or hide them at runtime

dlg_in_res_editor.png

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

add_circle_withfill.png

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.

Introduction

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.

Background  

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):

C++
// CDlg_AddObject Header file 

class CDlg_AddObject : public CDialog
{
// ... snipped ...
protected:
	CStackableGroupBox m_group_line;
	CStackableGroupBox m_group_circle;
	CStackableGroupBox m_group_rect;
	CStackableGroupBox m_group_fill;
// ... snipped ...
};
C++
// 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
	m_group_fill.show_group_expand_dialog
			(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));
	m_group_fill.show_group_expand_dialog
			(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.

Limitations

  • 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.

History

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

License

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


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

Comments and Discussions

 
GeneralI have the similar control Pin
Artem Moroz25-May-09 10:33
Artem Moroz25-May-09 10:33 
GeneralRe: I have the similar control Pin
Damir Valiulin26-May-09 6:55
Damir Valiulin26-May-09 6:55 
GeneralJust what I needed Pin
Christopher Latham12-Feb-09 13:50
Christopher Latham12-Feb-09 13:50 
Generalvery useful Pin
only_jack31-Jan-09 23:17
only_jack31-Jan-09 23:17 
GeneralRe: very useful Pin
Damir Valiulin1-Feb-09 7:34
Damir Valiulin1-Feb-09 7:34 
GeneralRe: very useful Pin
zzh122152-Feb-09 20:28
zzh122152-Feb-09 20: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.