Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

Child Dialog (Sub Forms)

Rate me:
Please Sign up or sign in to vote.
4.27/5 (8 votes)
17 Nov 1999 142.4K   3.7K   48   15
A simple class for handling Child Dialogs within a dialog or property sheet.

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


Written By
Chief Technology Officer
Switzerland Switzerland
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++ and today C#. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, from 2008 until 2013 for enValue (also Switzerland) and currently working for Comfone (Bern, Switzerland).

Married, two grown-up daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

Comments and Discussions

 
SuggestionChild Dialog Extra Pin
hessam amini14-Aug-13 3:15
professionalhessam amini14-Aug-13 3:15 
Generalseperate child dialog Pin
mohsen nourian7-Nov-04 19:24
mohsen nourian7-Nov-04 19:24 
GeneralRetrieve data from subform dialog Pin
Marker7913-Oct-03 5:05
Marker7913-Oct-03 5:05 
GeneralGet subform DialogCtrl Data Pin
triggerdie22-Apr-03 4:11
triggerdie22-Apr-03 4:11 
GeneralRe: Get subform DialogCtrl Data Pin
Rubizon7-Nov-07 4:19
Rubizon7-Nov-07 4:19 
GeneralRadio buttons cause hang / crash condition Pin
rothlink13-Apr-03 20:50
rothlink13-Apr-03 20:50 
GeneralRe: Radio buttons cause hang / crash condition Pin
silver831-Mar-08 11:32
silver831-Mar-08 11:32 
GeneralButtons and other Elements of Child Pin
pasci19-Mar-03 3:51
pasci19-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 dialog Pin
Simmyp2p8-Jan-03 0:54
Simmyp2p8-Jan-03 0:54 
GeneralProblem of UpdateData and DLL Pin
3-Nov-01 5:14
suss3-Nov-01 5:14 
GeneralTabctrl and DLL Pin
2-Nov-01 19:00
suss2-Nov-01 19:00 
GeneralHandling Windows messages Pin
Matt Soler17-Apr-00 11:31
sussMatt Soler17-Apr-00 11:31 
GeneralRe: Handling Windows messages Pin
Daniel Zuppinger17-Apr-00 19:22
Daniel Zuppinger17-Apr-00 19:22 
GeneralRe: Handling Windows messages Pin
Member 77227-Jul-00 15:39
Member 77227-Jul-00 15:39 
GeneralRe: Handling Windows messages Pin
ehomebuilder5-Feb-04 16:25
ehomebuilder5-Feb-04 16:25 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.