Get value of combobox in mfc

风流意气都作罢 提交于 2019-12-25 02:55:27

问题


I'm a beginner in C++ (MFC) programming. So, I have two comboboxes: IDC_COMBO1 and IDC_COMBO2 .

How can I simply get the selected by user value from them?

I've read the MSDN, but I didin't understand the examples:

int nIndex = m_pComboBox->GetCurSel();
int nCount = m_pComboBox->GetCount();

How must I declare m_pComboBox ?

Thanks for answer.


回答1:


CString sData;
int nSel = m_NameListBox.GetCurSel();
if (nSel != LB_ERR)
{
   m_NameListBox.GetText(nSel, sData);
}

Please also use DataExchange MFC mechanism to map your control to a class variable:

void CUserSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_NAME_LISTBOX, m_NameListBox);
}



回答2:


Here's the way you should declare m_pComboBox :

CComboBox *m_pComboBox1 = (CComboBox *) GetDlgItem(IDC_COMBO1);

Now, you can use both the line to get the current selection and number of selections as shown in question.



来源:https://stackoverflow.com/questions/29310465/get-value-of-combobox-in-mfc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!