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;
}

Downloads

Download demo project - 37.3 KB Kb


Comments

  • Powerful.

    Posted by Legacy on 06/26/2003 12:00am

    Originally posted by: Gigi

    I am headache for the same problem I have. Now I got the solution. Thanks

    Reply
  • Escape key and Enter key closes the inner dialog box, when it has focus

    Posted by Legacy on 04/04/2003 12:00am

    Originally 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?????
    Thanks,
    Sam

    Reply
  • Good Job!

    Posted by Legacy on 12/20/2002 12:00am

    Originally posted by: Davis

    It's very useful.
    Thanks a lot!

    Reply
  • Re: Why not use tabs?

    Posted by Legacy on 10/11/2002 12:00am

    Originally posted by: Jacques Du Biel

    Folks,
    The 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.

    Reply
  • will never be used

    Posted by Legacy on 12/01/2001 12:00am

    Originally 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.
    suggestion 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.

    Reply
  • Why not use tabs?

    Posted by Legacy on 11/13/2001 12:00am

    Originally 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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today, users, applications, and data exist in more places than ever before, creating an unprecedented challenge for IT. How can IT achieve the flexibility and agility it needs to offer multiple types of applications in multiple locations? To better serve business demands for information everywhere, enterprises must develop new strategies for optimizing multiple kinds of networks. Read this white paper to learn how hybrid networks provide an unprecedented level of network dynamism, enterprise agility, and the …

  • The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals: Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals. IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices. Job satisfaction is the No. 1 predictor of performance against organizational …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date