Skip to main content

Transferring Data From One Dialog Box to Another


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

Comments

Popular posts from this blog

Simple Windows Registry Access

The Windows registry is an excellent place to store program information. From recent file lists to program settings, the registry provides programmers with a central location to store information for future use. Registry access can be quite simple, provided that you accept a few limitations. First, you must be willing to store your registry values on a per-user basis (rather than for all users). Second, you may only read and write values to your own application's registry branch. Poking around in arbitrary places in the Windows registry is somewhat more complicated than the method provided in this article. Do not assume that the constraints mentioned above render this method of registry access useless. I personally use this registry access method for all of my applications, using the advanced access method only when necessary. The simple method described below will do everything you need it to do for simple storage of application specific information. Enabling Registry Access The ...

Application Context Menus

Context menus (or "right-click popup" menus) come in two flavors: application-wide or control-specific. This article focuses on the former. Implementing the Application Popup Menu The first step in creating a context menu is simple: use the Visual Studio resource editor to create the menu you wish to display. Make sure that you give it a unique ID; I will use the value IDR_POPUP_MENU for this example. Add to the menu the various menu items that you want to include. Note that your context menu should only have one top-level item (under which all other menu items will be placed). The caption for this top-level item can be whatever you like since the user will never see it. If you are mapping menu items in this context menu to commands that already exist in the program (as is likely), make sure that you give each menu item the appropriate ID. For example, if the ID to my "Open File" command in my main application menu is ID_FILE_OPEN, I would give that same ID to the c...