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.