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

Child Dialog (Sub Forms)

By , 17 Nov 1999
 

ChildDlg1.gif

ChildDlg1.gif

Introduction

A simple class for handling Child Dialogs within a dialog or property sheet. These Child Dialogs can be changed on the fly, much like subforms. It's a simple task really, but I got tired of programming everything every time :-(

Step 1. Design one or several 'Subdialogs'. Make sure to set the Style to Child and to tick the property 'Control'.

Step 2. Design a Parent Dialog. To make things easier, I add a static control for easier positioning of the child dialogs (normally I use a group frame).

Step 3. Generate for all dialogs, their classes.

Step 4. Add the files SubFormCollection.cpp and SubFormCollection.h to your project.

Step 5. Create an object of the class CSubFormCollection to the parent dialog.

 //
    CSubFormCollection m_SubForms;
 //

Step 6. In the OnInitDialog() method of the parent class, add the code below:

 //
    CRect r;
    // get the position for the subforms
    (GetDlgItem(IDC_SUBFORM_FRAME))->GetWindowRect(&r);
    // if the positioning is absolute use this, else the next line
    //    m_SubForms.SetRelPos(r);
    // centers the subdialog within the static IDC_SUBFORM_FRAME
    m_SubForms.SetCenterPos(r);
    m_SubForms.CreateSubForm(IDD_SUBFORM1,this); // create the sub forms
    m_SubForms.CreateSubForm(IDD_SUBFORM2,this);
    m_SubForms.CreateSubForm(IDD_SUBFORM3,this);
    m_SubForms.CreateSubForm(IDD_SUBFORM4,this);
    m_SubForms.ShowSubForm(); // show the first one
 //

Step 7. To switch between the subdialogs, use the code below:

 //
    m_SubForms.ShowSubForm(index_of_subform);
 //

Now the big work starts...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Daniel Zuppinger
Chief Technology Officer
Switzerland Switzerland
Member
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, currently employed by enValue (also Switzerland).
Current field is the project management and development of software for Bank applications.
Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalseperate child dialogmembermohsen nourian7 Nov '04 - 19:24 
hi
how we can call a child dialog (child.DoModal() ) from a parent ?
i mean the use of SetParent() and what other things it needs

GeneralRetrieve data from subform dialogmemberMarker7913 Oct '03 - 5:05 
Hi everyone,
 
After processing data in one of my four subforms, I want to pass this data to my main dialog by preferably pressing a button. Can anyone give me hint about how to accomplish this?
 
thanks in advance!,
Marker79
GeneralGet subform DialogCtrl Datamembertriggerdie22 Apr '03 - 4:11 
Hi,
 
This Class is very nice! But i have some Question to this.
 
How can i retrieve the Data out of a Control in a SubForm Dialog to my Main Dialog?!
 
e.g. I have a simple CEdit in my SubForm Dialog. and want to read it out to my Main Dialog Class where i declare the SubForms. Nothing works till now. I dont get the scope on the Elements(Controls).
 
So maybe somebody out there might have an Answer with a little bit of source would be great.WTF | :WTF:
 
Thanx to all in this community
 
Andre
GeneralRe: Get subform DialogCtrl DatamemberRubizon7 Nov '07 - 4:19 
yep got same problem
GeneralRadio buttons cause hang / crash conditionmemberrothlink13 Apr '03 - 20:50 
This is a great class, however it hangs when radio buttons are added to the parent (hosting) dialog. All other control types seem to work, but wouldn’t you know --- I need radio buttons for my design. I’m not sure if anyone else has seen this, but any assistance in resolving the anomaly be greatly appreciated.
 
TO REPRODUCE (in demo project):
1. Place two radio buttons in the IDD_TEST (parent/host) dialog.
2. Set the first radio button’s group property.
3. Rebuild and run.
4. Click either radio button.
5. App hangs somewhere in subsequent Windows’ message logic, after drawing the selected radio button.
 
...steven
 
P.S. Yes, I’ve included the latest modifications noted in the threads, but maybe I'm overlooking something. WTF | :WTF:
GeneralRe: Radio buttons cause hang / crash conditionmembersilver831 Mar '08 - 11:32 
Try putting
 
ModifyStyleEx(0, WS_EX_CONTROLPARENT);
 
in the ONInit method
GeneralButtons and other Elements of Childmemberpasci19 Mar '03 - 3:51 
Hi
I taked you subforms in a tab-gui. The switching between the form works fine - but buttons and edit-fields won't work.
Do you have any idea ?
GeneralScrolling Child dialogmemberSimmyp2p8 Jan '03 - 0:54 
How can i scroll the Image in the child dialogs which i have placed on the main dialog? I am using gdi+.Please help.Confused | :confused:
GeneralProblem of UpdateData and DLLmembermin2max3 Nov '01 - 5:14 
1. I'm using a dialog containing 2 subforms, when I update data from one of the subforms (UpdateData(TRUE)), an ASSERTion failed. Anybody have encountered similar problem and know why?
 
2. I also found that this class did not work in a MFC extensio DLL.
 
Any explanation and resolution will be appreciated.
 
Max
GeneralTabctrl and DLLmembermin2max2 Nov '01 - 19:00 
1. I have placed a tab contrl in one of the child dialogs managed by CSubFormCollection, but it did not display correctly.
2. I also found that this class did not work in a MFC extensio DLL.
 
Any explanation and resolution will be appreciated.
 
Max
GeneralHandling Windows messagessussMatt Soler17 Apr '00 - 11:31 
Have added a simple function which iterates through each CDialog window being managed by the CSubFormCollection and posts a message.
 
However it seems the messages never get there.
 
Try and trap a click event on a button you've added to a child dialog and you'll see what I mean.
 
Any help very much appeciated!
GeneralRe: Handling Windows messagessussDaniel Zuppinger17 Apr '00 - 19:22 
Sorry, but my code contains a error and I have not had the time to fix it. The problem is that you need to construct the CDialogs with new and then pass them to my class. e.g. :
(Code taken from a response I got from Jason Wilden)
 
bool CSubDlgCollection::CreateSubForm(CDialog *pDlg,UINT id, CWnd* pParentWnd)
{
ASSERT(pDlg != NULL);
if (pDlg == NULL) return(false);
 
// exit and tell the world about it
AddForm(pDlg); // Add it to the internal table
 
// Now create the dialog
pDlg->Create(id,pParentWnd);
 
....
}
 
The destructor also needs some modification:
 
CSubDlgCollection::~CSubDlgCollection()
{
// Free all sub forms on destruction
for (int i = 0; i <= m_Forms.GetUpperBound(); i++) {
CDialog* pDlg = m_Forms.GetAt(i);
pDlg->DestroyWindow();
}
}
 
The modified class is then used as follows :
 
CDlgNewProjectGroups *pDlg = new CDlgNewProjectGroups;
 
pDlg->m_bIncludeAllGroups = TRUE;
m_SubForms.CreateSubForm(pDlg,IDD_NEW_PROJECT_GROUPS,this); // create the sub forms
 
CDlgNewProjectSettings *pDlg2 = new CDlgNewProjectSettings;
m_SubForms.CreateSubForm (pDlg2,IDD_NEW_PROJECT_SETTINGS,this); // create
the sub forms
 
m_SubForms.ShowSubForm(); // show the first one
 
Regards Danie
GeneralRe: Handling Windows messagessussBo Su27 Jul '00 - 15:39 
Hi, Matt
 
Sorry to disturb you. I am reading your commends on Child dialog issue on CodeProject site.
 
I just want to know if you have met the problem that all child dialog dispear once you press "Enter" key.
 
Any suggestions are highly appricate.
 
Bo Su
GeneralRe: Handling Windows messagesmemberehomebuilder5 Feb '04 - 16:25 
About: press "Enter" key.
 
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
 
//CDialog::OnOK();
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 18 Nov 1999
Article Copyright 1999 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid