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

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. 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;
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, 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 */
10    /* Default values go to UI */
11    UpdateData(FALSE);
12  }
14  /* UpdateData with FALSE is called */
15  void CUserDialog::OnSubmit()
16  {
17    /* called by user */
18    /* UI values go to members */
19    UpdateData(TRUE);
20  }

mfc dodataexchange updatedata true false

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 Tue 29 Apr/2014 16:06:51(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.