CComboBox DDX_CBString behavior confusing

Viewed 989

I use a ComboBox control in a Dialog to give the user some useful values (Ex: 10; 20; 100; 400; 800) but let the user insert the exact value if needed.

What I have discovered after long time: If I type the value 40 in the Combobox, the Combobox returns after UpdataData() always 400. :(( Ohterwards with the values 39 or 41, there is no problem.

That's is not the behaviour what I and the user expected.
When I type a value, ComboBox should take this value, if select from the dropdown Menue, take this.

I see now this behaviour is given by DDX_CBString.

Do I have to write my own DDX_CBString or is there an another approach?

Code:

void CTestDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_IFBANDWIDTH, m_cIFBandWidth);
    DDX_CBString(pDX, IDC_IFBANDWIDTH, m_sIFBandWidth);  // Bahavior confusing
}

BOOL CTestDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    m_cIFBandWidth.ResetContent();

    m_cIFBandWidth.AddString(_T("10"));
    m_cIFBandWidth.AddString(_T("20"));
    m_cIFBandWidth.AddString(_T("100"));
    m_cIFBandWidth.AddString(_T("400"));
    m_cIFBandWidth.AddString(_T("800"));


    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

void CTestDialog::OnBnClickedApply()
{   
    UpdateData(TRUE);     // m_sIFBandWidth now 4 ok!
    UpdateData(FALSE);    // m_sIFBandWidth still 4, but control show 400, so the next OnOk() or Apply() take this value. Wrong!
}
2 Answers
Related