Transferring Data From One Dialog Box to Another

Data Transfer Data Transfer
In this exercise, we will learn how to exchange data from one dialog box to another.
  1. Start Microsoft Visual C++
  2. On the main menu, click File -> New...
  3. Click the Projects property sheet and click MFC AppWizard (exe)
  4. In the Location box, specify a complete path for the folder, such as C:\Programs\MSVC
  5. In the Project Name: box, type ExoTransfer and click OK
  6. In the MFC AppWizard - Step 1, click the Dialog Based radio button.
  7. Click Finish and click OK
  8. Design the default dialog box by adding an Edit Box IDentified as IDC_FIRSTNAMEDLG1
  9. Add a second Edit Box IDentified as IDC_LASTNAMEDLG1
  10. Add one more Edit Box IDentified as IDC_FULLNAMEDLG1
  11. Add a Button IDentified as IDC_CREATEACCOUNT and the Caption as C&reate Account...
     
    Dialog 1
  12. Set all three Edit controls read only by clicking the Read-Only check box of each (in the Edit Properties window).
  13. Press Ctrl + W to access the ClassWizard (I using Microsoft Visual C++ 5/6 for this exercise and not MSVC .NET)
  14. Click Member Variables and Add Variables for each edit box. Name them m_FirstNameDlg1, m_LastNameDlg1, m_FullNameDlg1 after their respective IDentifier
  15. Click OK
  16. In the Workspace, click the ResourceView property sheet and expand the ExoTransfer resources folder.
  17. Right-click the Dialog folder and click Insert Dialog
  18. Right-click the new dialog and click Properties
  19. Change the ID to IDD_DIALOG2
  20. Add two Edit Boxes IDentified respectively as IDC_FIRSTNAMEDLG2 and IDC_LASTNAMEDLG2 respectively
  21. Change the Caption of the IDOK button from OK to &Send...
     
    Dialog 2
  22. Press Ctrl + W to access the ClassWizard (you can use MSVC .NET if you want).
  23. You will be asked whether you want to create a new class. Click the Create A New Class radio button and click OK
  24. Change the name of the class to CDialog2 (that's the only thing you should change) and click OK.
  25. Click Member Variables.
  26. Add a Variable for each edit box. Name them m_FirstNameDlg2 and m_LastNameDlg2 according to their respective edit box.
  27. Click the Message Maps property sheet
  28. In the Class Name, select CExoTransferDlg. When asked whether you want to save your changes, click Yes.
  29. In the Object IDs list box, click IDC_CREATEACCOUNT
  30. In the Messages list box, double-click BN_CLICKED
  31. Change the Function Name to OnCreateAccount and press Enter
  32. Click Edit Code
  33. Scroll completely to the top of the file and add the header of the second dialog box
     
    // ExoTransferDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "ExoTransfer.h"
    #include "ExoTransferDlg.h"
    #include "Dialog2.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
  34. Scroll back down and implement the OnCreateAccount() member function as follows:
     
    void CExoTransferDlg::OnCreateAccount() 
    {
    	// TODO: Add your control notification handler code here
    	CDialog2 Dlg;
    
    	if( Dlg.DoModal() )
    	{
    		UpdateData();
    		m_FirstNameDlg1.Format("%s", Dlg.m_FirstNameDlg2);
    		m_LastNameDlg1.Format("%s", Dlg.m_LastNameDlg2);
    		m_FullNameDlg1.Format("%s %s", Dlg.m_FirstNameDlg2,
    					            Dlg.m_LastNameDlg2);
    		UpdateData(FALSE);
    	}
    }
  35. Press Ctrl + F5 to test the program

Download

Home