Drop List Controlled Dynamic Dialog Box | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 12, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.