Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0 down vote
favorite


I have a combobox in that I want to display different string on selecting an item in Combo.

My combo box is a dropdown combobox.

For eg: I have following in my combobox.

Alex - Manager

Rain - Project Lead

Shiney - Engineer

Meera - Senior Engineer

OnSelecting an item in combobox I want to diaply only name i.e. Alex.

I am able to achieve my requirement OnComboSelChange() and Arrow Keys event but on pressing enter key after using arrow keys in combo box, it is not showing formatted text in combo box.

What I have tried:

C++
struct details{
CString name;
CString des;
};

BOOL CComboTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

details d1;
d1.name = _T("alex");
d1.des =_T("manager");
m_vec.push_back(d1);

details d2;
d2.name = _T("Rain");
d2.des =_T("Engineer");
m_vec.push_back(d2);

// TODO: Add extra initialization here
for(int i=0;i<m_vec.size();i++)
{
m_ctrlCombo.AddString(m_vec[i].name+m_vec[i].des);
m_ctrlCombo.SetItemData(i,(DWORD_PTR)&m_vec[i]);
}
m_ctrlCombo.SelectString(-1,m_vec[0].name);
m_ctrlCombo.SetWindowText(m_vec[0].name);

return TRUE;  // return TRUE  unless you set the focus to a control
}

void CComboTestDlg::OnCbnSelchangeCombo1()
{
int nItem = m_ctrlCombo.GetCurSel();
details* det = (details*)m_ctrlCombo.GetItemData(nItem);
PostMessage(SETCOMBOTEXT,IDC_COMBO1,(LPARAM)(LPCTSTR)det->name);
}

BOOL CComboTestDlg::PreTranslateMessage(MSG* pMsg) 
{
MSG msg1=*pMsg;//I am loosing the value after  checking ..so storing temp.
MSG msg;
CopyMemory(&msg, pMsg, sizeof(MSG));
HWND hWndParent = ::GetParent(msg.hwnd);
while (hWndParent && hWndParent != this->m_hWnd)
{
msg.hwnd = hWndParent;
hWndParent = ::GetParent(hWndParent);
}
if (pMsg->message==SETCOMBOTEXT && (pMsg->wParam == IDC_COMBO1))
SetDlgItemText(IDC_COMBO1, (LPCTSTR)pMsg->lParam);
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN && msg.hwnd ==m_ctrlCombo.m_hWnd )
{
OnCbnSelchangeCombo1();
}
} 
return CDialog::PreTranslateMessage(pMsg);
}
Posted
Updated 14-Sep-17 21:39pm
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900