2016-03-15 56 views
3

我在對話框中使用ComboBox控件給用戶一些有用的值(例如:10; 20; 100; 400; 800),但讓用戶根據需要插入確切的值。CComboBox DDX_CBString行爲混淆

我很長一段時間後,發現了什麼: 如果我在組合框中鍵入值,UpdataData後的組合框返回()總是。 :(( Ohterwards與價值觀或,是沒有問題的

這不是我和用戶所期望的行爲
當我鍵入一個值,組合框應該把這個值。如果從下拉菜單中選擇Menue,藉此。

我現在看到的這種行爲是由DDX_CBString給出。

我必須寫我自己的DDX_CBString還是有一種另一種方法?

代碼:

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! 
} 
+0

如果您嘗試使用GetCurSel方法獲取當前索引,然後使用GetLBText方法獲取該索引處的文本,會發生什麼情況?那麼結果是什麼? –

+0

而不是我們猜測,您應該發佈適當的代碼,顯示您與組合框的交互。 – rrirower

+0

@AndrewTruckle:在上面的代碼示例中:在ComboBox中輸入4後,GetCurSel爲3,GetLBText爲「400」。 這不是我所期望的,它應該保持爲4. –

回答

2

我解決了這個與修改DDX_CBtring。

void DDX_CBString_Normal(CDataExchange* pDX, int nIDC, CString& value) 
{ 
    ..  
    if (pDX->m_bSaveAndValidate) 
    { 
     ..  
    } 
    else 
    { 
     // Behaviour as we expect: Type a value and keep it 
     // Select it form dropwon, or take the value the user type it 
     AfxSetWindowText(hWndCtrl, value); 
     return; 

     /* Disable original MS behavior 
     // set current selection based on model string 
     if (::SendMessage(hWndCtrl, CB_SELECTSTRING, (WPARAM)-1, 
      (LPARAM)(LPCTSTR)value) == CB_ERR) 
     { 
      // just set the edit text (will be ignored if DROPDOWNLIST) 
      AfxSetWindowText(hWndCtrl, value); 
     } 
     */ 
    } 
} 

如果有人有另一個aproach讓我知道。

+0

如果您查看選擇字符串方法的文檔,請訪問https://msdn.microsoft.com/en-us/library/30ft9e54.aspx它提到:僅當字符串的起始字符(從起點)時才選擇字符串。 –

+1

所以,它應該是FindStringExact,然後根據索引選擇它。否則,設置窗口文本。 DDX系統中的錯誤。 –

+0

40,不在列表中。但400是。所以選擇字符串選擇它。 –