Practical Learning Logo

Calling One Dialog Box From Another Dialog Box



It is unusual to have a dialog-based application. This is because a dialog box is usually made to complement a frame-based application. In that case, after adding the dialog box to the application, you would also provide the ability to call the dialog from an SDI. In the same way, if you create an application made of various dialog boxes, at one time, you may want the user to be able to call one dialog box from another. To start, you would create the necessary dialog boxes.

To call one dialog box from another, in the header file of the class of the calling dialog box, you can include the header file of the class of the called dialog box. In the section where you want to call the dialog box, you can declare a variable of that other dialog box' class and call its CDialog::DoModal() method.

Practical Learning Practical Learning: Calling One Dialog Box From Another

  1. Start Microsoft Visual C++ .NET or MS Visual Studio .NET
  2. Create a new MFC Application named Primary1
  3. Create it as Dialog Based
  4. Set the Dialog Title to Calling One Dialog Box From Another
  5. Click Finish
  6. To add another dialog box, on the main menu, click Project -> Add Resource...
  7. In the Add Resource dialog box, double-click Dialog
  8. Change the ID of the new dialog to IDD_SECOND_DLG and its Caption to Second
  9. Right-click the dialog box and click Add Class...
  10. Set the Class Name to CSecondDlg and base it on CDialog
  11. Click Finish
  12. From the Class View, double-click the IDD_PRIMARY1_DIALOG
  13. Using the Toolbox, add a Button to the dialog box and change its properties as follows:
    Caption: Second
  14. Double-click the new Second button and implement its event as follows:
    // Primary1Dlg.cpp : implementation file
    #include "stdafx.h"
    #include "Primary1.h"
    #include "Primary1Dlg.h"
    #include ".\primary1dlg.h"
    #include "SecondDlg.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    // CPrimary1Dlg dialog
    . . . No Change
    // The system calls this function to obtain 
    // the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CPrimary1Dlg::OnQueryDragIcon()
    	return static_cast<HCURSOR>(m_hIcon);
    void CPrimary1Dlg::OnBnClickedSecondBtn()
    	// TODO: Add your control notification handler code here
    	CSecondDlg Dlg;
  15. Test the application

Home Copyright © 2002-2005 FunctionX, Inc.