Dialog Data Exchange in MFC (2024)

  • Download demo project - 21.65 KB

Introduction

The MFC framework provides an efficient mechanism for transferring and validating data in a dialog box through the DDX and DDV routines. Dialog Data Exchange (DDX) is an easy way to initialize the controls in a dialog box and gather data input by the user. Dialog Data Validation (DDV) is an easy way to validate data entry in a dialog box.

The framework calls CWnd::DoDataExchange to exchange and validate dialog data. When a class is derived from CDialog, you need to override this member function if you wish to utilize the framework's automatic data exchange and validation. An implementation of DoDataExchange is shown below:

C++

void CDataExchangeDlg::DoDataExchange(CDataExchange* pDX){ //should precede the DDX and DDV routines  CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_strEdit); DDX_Check(pDX, IDC_CHECK1, m_bCheck);}

It's to be noted that DoDataExchange is never called directly, it's called by the CWnd::UpdateData member function.

C++

BOOL UpdateData( BOOL bSaveAndValidate = TRUE );

UpdateData is called to initialize data in a dialog box, or to retrieve and validate dialog data. The bSaveAndValidate flag indicates whether a dialog box is being initialized (set bSaveAndValidate = FALSE) or data is being retrieved (set bSaveAndValidate = TRUE).

//From wincore.cppBOOL CWnd::UpdateData(BOOL bSaveAndValidate){ BOOL bOK = FALSE; // assume failure TRY { DoDataExchange(&dx); bOK = TRUE; // it worked } CATCH(CUserException, e) { // validation failed - user already alerted, fall through ASSERT(!bOK); } return bOK;}

We see from the above that UpdateData() exists solely to create the data exchange object and to catch any exceptions and convert them into a boolean return value (where false indicates a failure in the exchange/validation process).

Let's have a look at the CDataExchange class declared in the afxwin.h, as well:

C++

//CDataExchange - for data exchange and validationclass CDataExchange{public: // Attributes BOOL m_bSaveAndValidate; // TRUE => save and validate data CWnd* m_pDlgWnd; // container usually a dialog // Operations (for implementors of DDX and DDV procs) HWND PrepareCtrl(int nIDC); HWND PrepareEditCtrl(int nIDC); void Fail(); // will throw exception CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate); // Implementation UINT m_idLastControl; // last control used (for validation) BOOL m_bEditLastControl; // last control was an edit item};

The m_bSaveAndValidate flag is the same as described above. Some other interesting members of the CDataExchange class are:

  • m_pDlgWnd: The window (usually a dialog) that contains the controls.
  • PrepareCtrl and PrepareEditCtrl: Prepares a dialog control for data exchange. Stores that control's handle for setting the focus if validation fails. PrepareCtrl is used for non-edit controls, and PrepareEditCtrl is used for edit controls. The PrepareEditCtrl first calls PrepareCtrl, and then sets m_bEditLastControl to TRUE. The reason to check whether the last control was an edit control is that the focus to the edit control is set by sending the EM_SETSEL message.
  • Fail: Called after bringing up a message box alerting the user to the input error. This routine will restore the focus to the last control (the last call to PrepareCtrl/PrepareEditCtrl) and throw an exception.

Dialog Data Exchange

MFC provides a library of transfer functions grouped under the heading of Dialog Data Exchange (DDX). One DDX function is defined for each type of control and each reasonable variable type. For example, the function to exchange data between an edit control and an integer variable is:

void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, int& value);

The CDataExchange parameter is used to hold context information during the exchange process. The ctrlID parameter holds the ID of an edit control, and value is a reference to a data member in the dialog class.

MFC overloads the DDX functions to handle parsing for different program variable types. For example, the DDX_Text() function has the following variations:

C++

void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, BYTE& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, short& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, int& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, UINT& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, long& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, DWORD& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, LONGLONG& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, ULONGLONG& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, CString& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, LPTSTR value, int nMaxLen);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, float& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, double& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, COleCurrency& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, COleDateTime& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, GUID& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, DECIMAL& value);void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, FILETIME& value);

MFC provides different DDX functions for different types of controls. For example, DDX_Check() works with check boxes, DDX_LBString() works with list boxes, etc. Look into afxdd_.h for all the available exchange and validation routines.

We can easily extend the DDX mechanism to work with new types. But before we do that, let's have a look at an implementation of DDX_Text(CDataExchange* pDX, int nIDC, CString& value) to understand how these routines work.

C++

//From dlgdata.cppvoid AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, CString& value){ HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC); if (pDX->m_bSaveAndValidate) { int nLen = ::GetWindowTextLength(hWndCtrl); ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1); value.ReleaseBuffer(); } else { AfxSetWindowText(hWndCtrl, value); //This is just a string helper function.  //It checks whether the text of the edit control //really changed, if yes then only it updates //its value, otherwise not (this helps reduce //the flash in the controls). }}

The code is simple and self-explanatory. Following that, we can set our own custom DDX routines. The following checks whether the user entered something in the edit control or not:

C++

void DDX_TextNotEmpty (CDataExchange* pDX, int nIDC, CString& value){ HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC); if (pDX->m_bSaveAndValidate) { int nLen = ::GetWindowTextLength(hWndCtrl); ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1); value.ReleaseBuffer(); if (value.IsEmpty ()) { AfxMessageBox (_T("Enter something in the text field."), MB_ICONSTOP); pDX->Fail (); //Fail () sets the focus on to the edit control  //and throws an exception } } else { ::SetWindowText(hWndCtrl, value); }}

Dialog Data Validation

In addition to providing transfer capabilities with dialog boxes, MFC also provides validation. Validation is the process of checking the values entered by the user against constraints. Example constraints could be to check whether a value is between the specified limits.

C++

void CDataExchangeDlg::DoDataExchange(CDataExchange* pDX){ //should precede the DDX and DDV routines  CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_nAge); DDV_MinMaxInt(pDX, m_nAge, 18, 45); DDX_Check(pDX, IDC_CHECK1, m_bCheck);}

The above code checks whether the value entered by the user lies between 18 and 45. If the value lies outside the specified limits, then the user is shown a message box to indicate the error, and Fail () is called (which sets the focus to the control and throws an exception). It's to be noted that a DDV routine for a given control must be called immediately after the DDX function for the same control (the reason being that, in case of a failure, the focus will be set to the offending control).

Like DDX routines, we can have our own custom DDV routines. The following checks whether the number is even or not.

C++

void DDV_IsEven(CDataExchange* pDX, int value) { if (value % 2 != 0) { AfxMessageBox("Enter an even number.", MB_ICONSTOP); pDX->Fail(); }}

History

  • 19th June, 2006: Initial post

This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Dialog Data Exchange in MFC (2024)

FAQs

Dialog Data Exchange in MFC? ›

The MFC

MFC
One quirk of MFC is the use of "Afx" as the prefix for many functions, macros and the standard precompiled header name "stdafx. h". During early development, what became MFC was called "Application Framework Extensions" and abbreviated "Afx".
https://en.wikipedia.org › Microsoft_Foundation_Class_Library
framework provides an efficient mechanism for transferring and validating data in a dialog box through the DDX and DDV routines. Dialog Data Exchange (DDX) is an easy way to initialize the controls in a dialog box and gather data input by the user.

How to convert data in dialog? ›

You can use the mydialog app and select the data plan you want. — If not you can use the #678# USSD code to select the data package. You can also send a message to whatsapp (777678678) and know more details about the data plans. If not you can visit dialog.lk too.

How do I add a dialog in MFC? ›

Step 1 − To create a dialog box, right-click on the Resource Files folder in solution explorer and select Add → Resource. Step 2 − In the Add Resource dialog box, select Dialog and click New. Step 3 − A dialog box requires some preparation before actually programmatically creating it.

What is the difference between DDX and DDV? ›

Dialog data exchange (DDX) is an easy way to initialize the controls in your dialog box and to gather data input by the user. Dialog data validation (DDV) is an easy way to validate data entry in a dialog box.

How to create modeless dialog box in mfc? ›

To create a modeless dialog box, call your public constructor and then call the dialog object's Create member function to load the dialog resource. You can call Create either during or after the constructor call. If the dialog resource has the property WS_VISIBLE, the dialog box appears immediately.

How to convert many to data in dialog? ›

You can do it through #678# and to choose the Mobile data plan option and select the preferred data plan. ... 3. — You can use the mydialog app and select the data plan you want. ...

How to get data from dialog? ›

Data Loan
  1. Step 1 - Dial #356# from your Dialog Mobile (prepaid connection)
  2. Step 2 - Select Option 1 "Ask for Loan"
  3. Step 3 - Select Option 3 “Data Loan-Prepaid”
  4. Step 4 - Select the applicable Loan (option 1 or 2)

How do I create a dialog in Dynamics 365? ›

Dialogs in Dynamics 365
  1. Click OK and it will open the dialog window:
  2. Click to add input arguments. ...
  3. Click to add a step and choose a page:
  4. Click to add variables:
  5. Choose the line and select Prompt and Response:
  6. Click Set Properties:
  7. Based on their answer, we can add a condition:
  8. If they answer no:
May 15, 2017

How do I create a dialog box? ›

Use the following procedure to create a custom dialog box:
  1. Create a UserForm. On the Insert menu in the Visual Basic Editor, click UserForm.
  2. Add controls to the UserForm. ...
  3. Set control properties. ...
  4. Initialize the controls. ...
  5. Write event procedures. ...
  6. Show the dialog box. ...
  7. Use control values while code is running.
Mar 29, 2022

Why is it called DDX? ›

In healthcare, a differential diagnosis (DDx) is a method of analysis that distinguishes a particular disease or condition from others that present with similar clinical features.

Why is DDX important? ›

A differential diagnosis is a helpful step in the process that your healthcare provider uses to make a final diagnosis. It may seem overwhelming to see a list of possible conditions that might affect you, but remember that your differential diagnosis is not the final diagnosis.

What is the DDX technique? ›

The differential process exists as follows:
  1. Step One: Identify and list the symptoms. ...
  2. Step Two: Consider and evaluate the most common diagnosis first. ...
  3. Step Three: List all possible diagnosis for the given symptoms. ...
  4. Step Four: Prioritize the list of candidate conditions by their severity.

What is the difference between dialog and modal? ›

A dialog still allows a user to interact with other content on the page while it's open (i.e. click buttons and links visible around the dialog) A modal locks down the page until something is done. This is typically done with a background that covers all other content so the modal must be dealt with.

What is the difference between modal and modeless dialog? ›

Modal dialog boxes, which require the user to respond before continuing the program. Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities.

How to hide MFC dialog? ›

You must hide the dialog from the inside.
  1. Overload OnInitDialog.
  2. Call CDialogEx::OnInitDialog()
  3. Hide your window and return.
Oct 12, 2012

How to change data package in dialog? ›

if you want to check the package change available please check through My dialog app or #679# and follow the process. by dialing #679# or via Mydialog App Contact an agent via whatsapp(0777 678 678) for further assistant.

How to change data to normal balance in dialog? ›

Once you activated a data package, you can't change it to the credit. Please contact dialog agent through 0777678678 WhatsApp number to get help. They will check and assist you.

How do you convert data? ›

The basic steps that most data conversions incorporate are as follows:
  1. A comprehensive plan is developed based on user requirements.
  2. The character/symbol set is extracted from its source.
  3. That source data is converted to the format of the destination.
  4. The data is reviewed and loaded to the target system.

How to make data dialog? ›

Activate via USSD

Dial #678#, and go to Mobile Data Plans and select Anytime Data Plans.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Kieth Sipes

Last Updated:

Views: 6159

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.