Dialog Data Exchange

MFC has a managed way to synch dialog member variables with UI control variables and vice versa. This is known as MFC Dialog Data exchange. Please read these two articles.

CDataExchange class

CDataExchange class : Explains details of the CDataExchange


DoDataExchange Dialog Data Exchange : Explains DoDataExchange in details. It tells more about how data exchange macros and win32 API which are used in the macros.

Implement Data Exchange

In MFC C++ managed approach we take meaningful dialog member variables in dialog class. Sametime dialog has corrosponding UI contols like textbox, checkbox, radio button, combo dropdown etc. One example with student application login dialog. I have two text box IDC_EDITUSER and IDC_EDITPASSWORD and one checkbox IDC_CHKREMEMBER. We have taken two CString type variable named as m_strUsername and m_strPassword for handling these text fields. There is also m_bRemember member which is a BOOL type variable to exchange data between checkbox.

class CStudentDlg : public CDialog
   virtual void DoDataExchange(CDataExchange* pDX);
    CString m_strUsername;
    CString m_strPassword;
    BOOL    m_bRemember;

void CStudentDlg::DoDataExchange(CDataExchange* pDX)
   DDX_Text(pDX, IDC_EDITUSER, m_strUsername);
   DDX_Text(pDX, IDC_EDITPASSWORD, m_strPassword);
   DDX_Check(pDX, IDC_CHKREMEMBER, m_bRemember);


We set initial default values of data members in dialog constructor. MFC framework calls UpdateData(FALSE) before OnInitDialog handler. Framework's DDX mechanism transfers the values of the member variables to the controls in the dialog box using virtual function DoDataExchange(). The default implementation of OnInitDialog in CDialog calls the UpdateData member function of class CWnd to initialize the controls in the dialog box.

UpdateData and DoDataExchange

UpdateData works in both directions, as specified by the BOOL parameter passed to it. To carry out the exchange, UpdateData sets up a CDataExchange object and calls your dialog class's override of CDialog's DoDataExchange member function. DoDataExchange takes an argument of type CDataExchange. The CDataExchange object passed to UpdateData represents the context of the exchange, defining such information as the direction of the exchange. The purpose of UpdateData() function is to update member variable with the values from UI controls. This is done with an argument value of TRUE. However a FALSE parameter to this function inverses the flow and UI controls are updated with dialog member variables.

BOOL CWnd::UpdateData(BOOL bSaveAndValidate)
   CDataExchange DX(this, bSaveAndValidate);
/* UpdateData with TRUE is called */
BOOL CDialog::OnInitDialog()
  /* called by MFC framework */
  /* Default values go to UI */

/* UpdateData with FALSE is called */
void CUserDialog::OnSubmit()
  /* called by user */
  /* UI values go to members */

mfc dodataexchange updatedata true false

Data exchange macros

All data exchange macros has been explained.

Data validation macros

Data valivation and custom error messages.

Custom validation message box

Custom validation message box.

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...