问题
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