Switching dialog boxes in a dialog-based application
A dialog-based application is an application that uses a CDialog box as it's mainframe, rather than the familier mainframe-document-view architecture. Here is a nice simple trick you can use in programming such an application: Suppose you want to use two dialog-boxes in your application and you have to use both dialog-box in your main window. You can use only one CDialog class at a time as your main application, but you can switch it with the other CDialog box any time you want. Here is how to do it:
1. Create your dialog-application with the class-wizard. In step 1 - choose the third option "Dialog based". (In The other steps just use the defult options). Lets assume the name of the application is "Test".
2. Go to the Resource View and click on the Dialog box - "IDD_TEST_DIALOG" (or whatever your Dialog-box resource ID is).
3. Add a control Button, write inside "Switch" and set it's ID to IDC_SWITCH.
4. Create another Dialog box and add a control button there too. Name it "Switch back" and set it's ID to IDC_SWITCHBACK.
5. Now, start Class-Wizard (Ctrl-w) and look for class CTestDlg, which is the main CDialog box of the application. Find the IDC_SWITCH ID in the Object ID's table, than select the message "BN_CLICKED", and add the function "OnSwitch" to your application.
6. Go to the second dialog box, create a new class to associate with it (I'm calling it CSecondDlg), and perform step 5 to the IDC_SWITCHBACK button.
7. Now go to the file of the main Dialog application, TestDlg.cpp and add to your include lines - #include "CSecondDlg.h". Now, get to the function "OnSwitch" that you added previously and add the following code:
void CTestDlg::OnSwitch()
{
// First get a pointer to the main pointer - m_pMainWnd of the application
CWnd* TheMainWindow = AfxGetMainWnd();
// Cancel the current dialog box
CDialog::OnCancel;
// Define a new class for the second dialog class
CSecondDlg CSecDlg;
// Set the pointer of the application to the second dialog box
TheMainWnd = &CSecDlg
// Run the second dialog box in the main application
CSecDlg.DoModal();
}
8. Get to the file CSecDlg.cpp - the file of the second Dialog box, and do the same thing, just the other way around: Add the line #include "CTestDlg.h" in the inlude lines, get to the OnBackSwitch function and write:
void CSecondDlg::OnBackSwitch()
{
// First get a pointer to the main pointer - m_pMainWnd of the application
CWnd* TheMainWindow = AfxGetMainWnd();
// Cancel the current dialog box
CDialog::OnCancel;
// Define a new class for the first CTest dialog class
CTestDlg CFirstDlg;
// Set the pointer of the application to the first dialog box
TheMainWnd = &CFirstDlg
// Run the first dialog box in the main application
CFirstDlg.DoModal();
}
NOTES:
1. You can do this with 3, 4 or as much Dialog boxes as you wish.
2. If you need to keep specific data that is relevent to one of the dialog boxes for latter use - I suggest you create data variables in the CWinApp class of your application and store there your specific data while the Dialog box is inactive, and restore the data again when it's active, by using AfxGetApp();
Good luck, Amir.

Comments
Awsome
Posted by Legacy on 04/14/2003 12:00amOriginally posted by: Graham
thanks for the help, I searched for so long trying to find a solution to my problems, thanks so much!
ReplyQuestion
Posted by Legacy on 11/13/2002 12:00amOriginally posted by: ArielR
ReplyAccess the controls of one dialog within another
Posted by Legacy on 06/26/2002 12:00amOriginally posted by: Utkal Ranjan
Hi
Can u tell me how can I access the controls(properties+functions) from one dialog from another dialog ? My Dialog Based Application has lots of Dialogs (abt 50 dialogs). I want to know a specific property of a control with in another dialog. Plz tell me.
Another thing is that who can guide me to know more abt VC++. I want to communicate with him by Yahoo Messaenger. My Messenger ID is "ctboy420". I'll be happy if any one can help me.
ReplyMFC CRecordView
Posted by Legacy on 03/07/2002 12:00amOriginally posted by: saro
ReplyExcellent, well explained idea. Cheers...
Posted by Legacy on 01/18/2002 12:00amOriginally posted by: Lee Tobin
Pitty about the typo's :)
ReplyUSING MEMBER FUNCTIONS OF ONE DIALOG IN ANOTHER(URGENT)
Posted by Legacy on 12/15/2001 12:00amOriginally posted by: amit
Hi All!
I have created a dialog based application in mfc.
i need to use the controls of one dialog in the another dialog so that they are interdependent on each other i.e;
the functions should behave in the same manner.
My problem is that when i switch the dialogs the operations done in the main view are lost.I need the functions as if they are accessible from all dialogs and no data is lost while dialog switching.
I have used modal dialogs.
Please advise.
ReplyThanks & regards
amit
Please suggest a solution.......
Posted by Legacy on 11/10/2001 12:00amOriginally posted by: Rupak
I am facing a problem in the following situation...
I have 4 dialogs say A,B,C,D.Out of which, B needs to be
shown all the time as tray icon.Now I should be able to
show C and/or D with B as Modal dialog.How do I achieve
this? Also,with B already there,C and D dialogs remain on
top of all windows even if they have lost their
focus,where as I would like them to behave as normal
windows.
Any help would be greatly appreciated.
Thanking in appreciation,
ReplyRupak.
how do you make the 2nd one show in taskbar?
Posted by Legacy on 10/23/2000 12:00amOriginally posted by: Jack
My project has 2 dialogs and when the user clicks on a button I want the 2nd dialog to open and the first to close. I can do this fine except the 2nd dialog doesn't have an icon and doesn't show in the windows task bar. Any suggestions?
ReplyFix Compile Error
Posted by Legacy on 05/18/2000 12:00amOriginally posted by: MyeongHoon Lee
I changed the OnSwitch and the OnSwitchBack functions as follows:
Old coding
CWnd* TheMainWindow = AfxGetMainWnd();
CDialog::OnCancel;
New coding
CWnd* TheMainWnd = AfxGetMainWnd();
CDialog::OnCancel();
// change TheMainWindow -> TheMainWnd
Reply// change OnCancel -> OnCancel()
//Thanks
TheMainWindow not being used.
Posted by Legacy on 01/20/2000 12:00amOriginally posted by: Wang Quan
ReplyLoading, Please Wait ...