Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

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

// 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 ...
};
// 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)

Share

About the Author

Damir Valiulin
Software Developer
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralI have the similar control PinmemberM A V25-May-09 10:33 
GeneralRe: I have the similar control PinmemberDamir Valiulin26-May-09 6:55 
GeneralJust what I needed PinmemberChristopher Latham12-Feb-09 13:50 
Generalvery useful Pinmemberonly_jack31-Jan-09 23:17 
GeneralRe: very useful PinmemberDamir Valiulin1-Feb-09 7:34 
GeneralRe: very useful Pinmemberzzh122152-Feb-09 20:28 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 1 Feb 2009
Article Copyright 2009 by Damir Valiulin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid