UI Data exchange

Win32 subsystem provides a wide range of UI controls and MFC framework has DDX or dialog data exchange macro for each type of UI controls. Here we are discussing the data exchange macros and suitable member variables in dialog class for most common UI controls available in Win32.

EditBox

Editbox mostly contains a simple text and CString is mostsuitable Data ehchanged in an editbox. However data can be in the form of integer, floating point numbers. Morefover formatted data can be exchanged in form of currency, date and time, GUID, file time etc.

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);

Checkbox

Checkbox can have two states - checked or unchecked. Thus a BOOL/boolean/integer is suitable variable for exhange.

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

Radio button

Radio button is like Checkbox can have two states - checked or unchecked. Thus a BOOL/boolean/integer is suitable variable for exhange.

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

Combo box

Combo box is a single selection and thus integer can be used to check the position of the selection or a string can be used to get the selected index text.

void AFXAPI DDX_CBString(CDataExchange* pDX, int nIDC, CString& value);
void AFXAPI DDX_CBIndex(CDataExchange* pDX, int nIDC, int& index);
void AFXAPI DDX_CBStringExact(CDataExchange* pDX, int nIDC, CString& value);

List box - single selection

A single selection is same like a Combo box. An integer can be used to check the position of the selection or a string can be used to get the selected index text.
void AFXAPI DDX_LBString(CDataExchange* pDX, int nIDC, CString& value);
void AFXAPI DDX_LBIndex(CDataExchange* pDX, int nIDC, int& index);
void AFXAPI DDX_LBStringExact(CDataExchange* pDX, int nIDC, CString& value);

List box - multi selection

List box often used for getting multiple selections. Data exchange is little bit complex when multiple selection is Involved. An array of CString or Integer is used. There is no MFC define DDX macro and user can write a function as below.

void AFXAPI DDX_LBStringArray(CDataExchange* pDX, int nIDC, CStringArray& value)
{
    HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
    if (pDX->m_bSaveAndValidate)
    {
      char szSelection[64];
      CArray <int , int> aListBoxSel;
      int nNbItems = ListBox_GetSelCount(hWndCtrl);
      aListBoxSel.SetSize(nNbItems);
      ListBox_GetSelItems(hWndCtrl, nNbItems, aListBoxSel.GetData());
      value.RemoveAll();

      for (int i=0; i < nNbItems;i++)
      {
        ListBox_GetText(hWndCtrl, aListBoxSel.GetAt(i), szSelection);
        value.Add(szSelection);
      }
    }
    else
    {
    for (int i = 0; i < value.GetSize();i++)
    ListBox_AddString(hWndCtrl,value.GetAt(i));
    }
}

Scrollbar

Scrollbar holds a position and thus integer can be used.

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

Slider

Slider is very similer to Scrollbar which holds a position and thus integer can be used.

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

IP Address control

IP address (IPv4) is a unsigned 32bit integer splitted in four parts by BYTE in for small boxes in IP address controller. A 32bit unsigned integer or DWORD is used for data exchange.

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

Month Calender

Month Calender contains a date time and MFC has CTime, COleDateTime, FILETIME etc as data type.

void AFXAPI DDX_MonthCalCtrl(CDataExchange* pDX, int nIDC, CTime& value);
void AFXAPI DDX_MonthCalCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value);
void AFXAPI DDX_MonthCalCtrl(CDataExchange* pDX, int nIDC, FILETIME& value);

Date Time control

Date Time control like Month Calender contains a date time and MFC has CString, CTime, COleDateTime, FILETIME etc as data type.

void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, CString& value);
void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, CTime& value);
void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value);
void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, FILETIME& value);

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 ...

#