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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds