In this exercise, we will learn how to exchange data from
one dialog box to another.
- Start Microsoft Visual C++
- On the main menu, click File -> New...
- Click the Projects property sheet and click MFC AppWizard (exe)
- In the Location box, specify a complete path for the folder, such as C:\Programs\MSVC
- In the Project Name: box, type ExoTransfer
and click OK
- In the MFC AppWizard - Step 1, click the Dialog Based radio button.
- Click Finish and click OK
- Design the default dialog box by adding an Edit Box IDentified as
IDC_FIRSTNAMEDLG1
- Add a second Edit Box IDentified as IDC_LASTNAMEDLG1
- Add one more Edit Box IDentified as IDC_FULLNAMEDLG1
- Add a Button IDentified as IDC_CREATEACCOUNT and the Caption as
C&reate Account...
- Set all three Edit controls read only by clicking the Read-Only
check box of each (in the Edit Properties window).
- Press Ctrl + W to access the ClassWizard (I using Microsoft Visual
C++ 5/6 for this exercise and not MSVC .NET)
- Click Member Variables and Add Variables for each edit box. Name
them m_FirstNameDlg1, m_LastNameDlg1,
m_FullNameDlg1 after their
respective IDentifier
- Click OK
- In the Workspace, click the ResourceView property sheet and expand
the ExoTransfer resources folder.
- Right-click the Dialog folder and click Insert Dialog
- Right-click the new dialog and click Properties
- Change the ID to IDD_DIALOG2
- Add two Edit Boxes IDentified respectively as IDC_FIRSTNAMEDLG2 and
IDC_LASTNAMEDLG2 respectively
- Change the Caption of the IDOK button from OK to &Send...
- Press Ctrl + W to access the ClassWizard (you can use MSVC .NET if
you want).
- You will be asked whether you want to create a new class. Click the
Create A New Class radio button and click OK
- Change the name of the class to CDialog2
(that's the only thing you should change) and click OK.
- Click Member Variables.
- Add a Variable for each edit box. Name them m_FirstNameDlg2 and
m_LastNameDlg2 according to their respective edit box.
- Click the Message Maps property sheet
- In the Class Name, select CExoTransferDlg. When asked whether you
want to save your changes, click Yes.
- In the Object IDs list box, click IDC_CREATEACCOUNT
- In the Messages list box, double-click BN_CLICKED
- Change the Function Name to OnCreateAccount and press Enter
- Click Edit Code
- 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
|
- 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);
}
}
|
- Press Ctrl + F5 to test the program
Download
|