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:00am

    Originally posted by: Graham

    thanks for the help, I searched for so long trying to find a solution to my problems, thanks so much!

    Reply
  • Question

    Posted by Legacy on 11/13/2002 12:00am

    Originally posted by: ArielR

    I have created 3 dialogs,
    
    Dlg1-> Parent
    Dlg2 -> child
    Dlg3 -> child

    How I may to do if want to use Dlg2 members like CString value in Dlg3 dialog.

    I tried with:
    In Dlg3:
    # Include "Dlg2.h"

    BOOL CDlg3::OnInitDialog()
    {
    CDialog::OnInitDialog();

    CDlg2 m_pDlg2;

    m_sDlg3Strn = m_pDlg2.m_sTxtDlg2;

    UpdateData(TRUE);

    return TRUE; // return TRUE unless you set the
    }

    But my m_pDlg2.m_sTxtDlg2 String is empty.

    Reply
  • Access the controls of one dialog within another

    Posted by Legacy on 06/26/2002 12:00am

    Originally 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.

    Reply
  • MFC CRecordView

    Posted by Legacy on 03/07/2002 12:00am

    Originally posted by: saro

    Hi your example is good
    
    can u say how i ll do the exchange for CRecordView?

    Reply
  • Excellent, well explained idea. Cheers...

    Posted by Legacy on 01/18/2002 12:00am

    Originally posted by: Lee Tobin

    Pitty about the typo's :)

    Reply
  • USING MEMBER FUNCTIONS OF ONE DIALOG IN ANOTHER(URGENT)

    Posted by Legacy on 12/15/2001 12:00am

    Originally 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.


    Thanks & regards
    amit

    Reply
  • Please suggest a solution.......

    Posted by Legacy on 11/10/2001 12:00am

    Originally 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,
    Rupak.

    Reply
  • how do you make the 2nd one show in taskbar?

    Posted by Legacy on 10/23/2000 12:00am

    Originally 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?

    Reply
  • Fix Compile Error

    Posted by Legacy on 05/18/2000 12:00am

    Originally 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
    // change OnCancel -> OnCancel()
    //Thanks

    Reply
  • TheMainWindow not being used.

    Posted by Legacy on 01/20/2000 12:00am

    Originally posted by: Wang Quan

    As a matter of fact, the point of TheMainWindow is not used.
    
    There will be the same result if you cancel the following:

    // CWnd* TheMainWindow = AfxGetMainWnd();
    // TheMainWindow = &Yourdlg1;

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds