Add Variable - MFC Control

Let's take ListBox and Edit as examples.

2025-11-10

Written by: xiaobin

Add Variable:

DDX Functions

NameDescription
DDX_CBIndexInitializes or retrieves the index of the current selection of a combo box control.
DDX_CBStringInitializes or retrieves the current contents of the edit field of a combo box control.
DDX_CBStringExactInitializes or retrieves the current contents of the edit field of a combo box control.
DDX_CheckInitializes or retrieves the current state of a check box control.
DDX_ControlSubclasses a given control within a dialog box.
DDX_DateTimeCtrlInitializes or retrieves date and/or time data of a date and time picker control.
DDX_IPAddressInitializes or retrieves the current value of an IP address control.
DDX_LBIndexInitializes or retrieves the index of the current selection of a list box control.
DDX_LBStringInitializes or retrieves the current selection within a list box control.
DDX_LBStringExactInitializes or retrieves the current selection within a list box control.
DDX_ManagedControlCreates a .NET control matching the control’s resource ID.
DDX_MonthCalCtrlInitializes or retrieves the current value of a month calendar control.
DDX_RadioInitializes or retrieves the 0-based index of the radio control that is currently checked within a radio control group.
DDX_ScrollInitializes or retrieves the current position of a scroll control’s thumb.
DDX_SliderInitializes or retrieves the current position of a slider control’s thumb.
DDX_TextInitializes or retrieves the current value of an edit control.

ListBox

    m_Types.AddString(_T("MS-Access"));
    m_Types.AddString(_T("MSSQL"));
    m_Types.AddString(_T("MySQL"));

for example:

    int iCurSel = 0;
    iCurSel = m_Types.GetCurSel();
    CString tmp;
    tmp.Format(_T("%d"), iCurSel);
    AfxMessageBox(tmp);

Edit

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT1, m_Server);
}

change:

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_Server);
}

Otherwise, the following will occur:

error C2664: 'void DDX_Control(CDataExchange *,int,CWnd &)' : cannot convert argument 3 from 'CString' to 'CWnd &'
    m_Types.GetText(iCurSel, tmp);
    m_Server = tmp;
    // Update the display
    UpdateData(FALSE);

Ref