Drop List Controlled Dynamic Dialog Box
Environment: VC6, win2000
It is not uncommon to need a dialog box that can change its appearance based on selections made within the dialog box. (Hence the need for tab and property sheets.) Often, tab controls or property sheets can be used to accomplish such dynamic behavior. Sometimes, however, there are so many possible selections and combinations of selections, that tab or property sheets are impractical. This technique shows a simple example of how to dynamically change a dialog box based on the selection of a list box. The CDynamicDlgDlg::Initialize() method shown below is called from the CDynamicDlgDlg::OnInitDialog() method. Each of the CDialogs in the CDialogStructure shown below is a dialog class with CHILD and THIN_FRAME properties. This could also be done in an array of CDialogs, but then functions and attributes specific to the "new" dialog classes would not be available.
typedef struct
{
CButtonDlg buttonDlg;
CRadioDlg radioDlg;
CStaticTextDlg staticTextDlg;
}CDialogStructure;
typedef enum
{
BUTTON_DLG,
RADIO_DLG,
STATIC_TEXT_DLG,
NUM_DLGS
}eDlgType;
void CDynamicDlgDlg::Initialize()
{
CDialog* dlg;
numDlgs = NUM_DLGS;
currentDlg = BUTTON_DLG;
CRect currentDlgRect, parentDlgRect;
dialogStructure.buttonDlg.Create(IDD_BUTTON_DLG, this);
dialogStructure.radioDlg.Create(IDD_RADIO_DLG, this);
dialogStructure.staticTextDlg.Create(IDD_STATIC_TEXT_DLG, this);
GetClientRect(parentDlgRect); //parentDlgRect = GetMyRectangle();
for(int i = 0; i < NUM_DLGS; i++)
{
dlg = GetDlgAt(currentDlg);
dlg->GetClientRect(currentDlgRect);
dlg->SetWindowPos((const CWnd*)&wndTop,
(parentDlgRect.right-currentDlgRect.right)/2, \
(parentDlgRect.bottom - currentDlgRect.bottom)/2, \
currentDlgRect.right,
currentDlgRect.bottom,
SWP_SHOWWINDOW);
dlg->ShowWindow((i==BUTTON_DLG)?SW_SHOW:SW_HIDE);
currentDlg = currentDlg + 1;
}
currentDlg = BUTTON_DLG;
}
CDialog* CDynamicDlgDlg::GetDlgAt(int dlgType)
{
switch(dlgType)
{
case BUTTON_DLG:
return &dialogStructure.buttonDlg;
case RADIO_DLG:
return &dialogStructure.radioDlg;
case STATIC_TEXT_DLG:
return &dialogStructure.staticTextDlg;
default:
return NULL;
}
}
void CDynamicDlgDlg::OnSelchangeDialogDropList()
{
CDialog* dlg;
UpdateData(true);
dlg = GetDlgAt(currentDlg);
dlg->ShowWindow(SW_HIDE);
dlg = GetDlgAt(dialogType);
dlg->ShowWindow(SW_SHOW);
currentDlg = dialogType;
}

Comments
Powerful.
Posted by Legacy on 06/26/2003 12:00amOriginally posted by: Gigi
I am headache for the same problem I have. Now I got the solution. Thanks
ReplyEscape key and Enter key closes the inner dialog box, when it has focus
Posted by Legacy on 04/04/2003 12:00amOriginally posted by: sam
Hi,
There is a serious problem, when we use this approach!
for instance, in this example, in the radio button dialog box, select one of the radio button and then press escape key or enter key, that radio button dialog box vanishes in the air, but the parent dialog box still exists.
Is there any solution, anybody can give?????
ReplyThanks,
Sam
Good Job!
Posted by Legacy on 12/20/2002 12:00amOriginally posted by: Davis
It's very useful.
ReplyThanks a lot!
Re: Why not use tabs?
Posted by Legacy on 10/11/2002 12:00amOriginally posted by: Jacques Du Biel
Folks,
ReplyThe comments raised here are all valid. Yes, using Property Sheets and Property Pages may be the preferred way to go as specified in Windows UI design guidelines. Let's consider this : Why do Microsoft deviate from the guideline regarding this approach? You can simply have a look at the Scheduled Task Properties dialog in 'Scheduled Tasks'. It follows this exact approach. Is it the best way to implement this functionality? Probably not. The fact of the matter is, it may be required or even desired in some cases.
will never be used
Posted by Legacy on 12/01/2001 12:00amOriginally posted by: ugly
what you did was an attempt to rewrite property sheet, but in ugly way and waste of time. if you think that having lots of property pages is not the way to go (while you are indeed need them) go back to procedural languages.
Replysuggestion is to improve what we have with property sheet with features, such as side tabs, toolbars, tree control (all are on property sheet area) for reloading different set of pages, etc.
keep looking in codeguru, you find lots of excellent ideas and sample codes. sure.
Why not use tabs?
Posted by Legacy on 11/13/2001 12:00amOriginally posted by: Peter Ritchie
Why would you not use tabs in a circumstance like this?
The Windows UI design guildlines would suggest that a Tab control should used in circumstances like this.
You're overridding the basic functionality of a combo-box/dropdown and causing it to perform (or to have a side-effect) that is not common. The use of a dropdown in this way is counter-intuitive. This makes the interface harder to understand and learn--without adding any more value.
Reply