EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Win32 Dialog Data Exchange

Dialog window is the top level window and dialog controles like button, editbox, checkbox, combobox, radio buttons, list box are child windows. These child controls are identified by an identity number or ID. There are Win32 API to retrive values from the UI controls as well as there are APIs for appliying values to UI controls. Each UI controls holds different types information in it thus exchange of data sould be in different types of variables. Lets discuss data exchange routines of UI controls editbox, checkbox, combobox, radio buttons, list box one by one.

User can can retrieve strings, integer, BOOL etc using these calls

int GetWindowText(
HWND   hWnd,
LPTSTR lpString,
int    nMaxCount

UINT GetDlgItemText(
HWND   hDlg,
int    nIDDlgItem,
LPTSTR lpString,
int    nMaxCount

UINT GetDlgItemInt(
HWND hDlg,
int  nIDDlgItem,
BOOL *lpTranslated,
BOOL bSigned

UINT IsDlgButtonChecked(
HWND hDlg,
int  nIDButton

These Win32 API are used to apply text or set the state of the button from variable.

BOOL SetWindowText(
HWND    hWnd,
LPCTSTR lpString

void SetDlgItemInt(
HWND hDlg,
int nID,
UINT nValue,
BOOL bSigned
void SetDlgItemText(
HWND hDlg,
int nID,
LPCTSTR lpszString);
void CheckDlgButton(
HWND hDlg,
int nIDButton,
UINT nCheck

void CheckRadioButton(
HWND hDlg,
int nIDFirstButton,
int nIDLastButton,
int nIDCheckButton

MFC Dialog Data Exchange

MFC applications can use these same APIs wrapped in CWnd class. These APIs often require additional variables to exchange a single value. MFC however has a managed way to exchange data. This is known as MFC Dialog Data exchange. MFC data exchange is not an indipendent way however above Win32 APIs are useed to construct these MFC data exchange macros.

MFC uses C++ style 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
public :
   virtual void DoDataExchange(CDataExchange* pDX);
private :
    CString m_strUsername;
    CString m_strPassword;
10      BOOL    m_bRemember;
11  };
13  void CStudentDlg::DoDataExchange(CDataExchange* pDX)
14  {
15     CDialog::DoDataExchange(pDX);
16     DDX_Text(pDX, IDC_EDITUSER, m_strUsername);
17     DDX_Text(pDX, IDC_EDITPASSWORD, m_strPassword);
18     DDX_Check(pDX, IDC_CHKREMEMBER, m_bRemember);
20  };

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, text or BOOL data can be fetched from UI or can be applied to the UI. 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.

mfc dodataexchange updatedata true false

Code wizard can help users to add data exchange entries in DoDataExchange() function. User can specify member variable name graphically for each controls in the dialog. Code wizard automatically adds DDX functions for each member-control pair in DoDataExchange(). Each DDX function knows how to exchange data in both directions based on the context supplied by the CDataExchange argument passed to your DoDataExchange by UpdateData. MFC provides many DDX functions for different kinds of exchange. The following example shows a DoDataExchange override in which two DDX functions for TEXT BOX and CHECK BOX controls.

void CStudentDlg::DoDataExchange(CDataExchange* pDX)
   DDX_Text(pDX, IDC_EDITUSER, m_strUsername); /*Exchange for User name */
   DDX_Text(pDX, IDC_EDITPASSWORD, m_strPassword);  /*Exchange for Password */
   DDX_Check(pDX, IDC_CHKREMEMBER, m_bRemember); /*Exchange for remember */

DDX_Text using Win32 API

Here we have written our own DDX routine and placed in DoDataExchange(). DDX_MyText() function is written with the help of Win32 APIs like GetWindowText(), GetWindowTextLength(), SetWindowText(). This is to show how DoDataExchange() and DDX routines can be written with Win32 APIs. However MFC implements DDX_Text() inside their framework with same logic and APIs as below.

void DDX_MyText(CDataExchange* pDX, int nIDC, CString& value)
  HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  /* Window to variable exchange */
  if (pDX->m_bSaveAndValidate)
    int length;
    LPTSTR buffer;
    length = GetWindowTextLength(hWndCtrl);
10      buffer = value.GetBufferSetLength(length + 1);
11      GetWindowText(hWndCtrl, buffer, length+1);
12    }
13    /* Variable to Window/UI exchange */
14    else
15    {
16      SetWindowText(hWndCtrl, (LPCTSTR)value);
17    }
18  }
19  void CStudentDlg::DoDataExchange(CDataExchange* pDX)
20  {
21     CDialog::DoDataExchange(pDX);
22     DDX_MyText(pDX, IDC_EDITUSER, m_strUsername);
23     DDX_MyText(pDX, IDC_EDITPASSWORD, m_strPassword);
24     DDX_Check(pDX, IDC_CHKREMEMBER, m_bRemember);
26  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 05:58:02(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.