您的位置:首页 > 编程语言 > C语言/C++

[ VC++ ]关于ComboBox的 GetCurSel() 改变和它关联的CString变量 为什么得到的不是 -1

2006-01-02 20:55 731 查看
ComboBox直接输入的时候会自动选择匹配的列表项,SetWindowText则不会
---------------------------------------------------------------

//得到ComboBox的文字
void GetComboBoxString(HWND hWndCtrl, CString& value)
{
// just get current edit item text (or drop list static)
int nLen = ::GetWindowTextLength(hWndCtrl);
if (nLen > 0)
{
// get known length
::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1);
}
else
{
// for drop lists GetWindowTextLength does not work - assume
// max of 255 characters
::GetWindowText(hWndCtrl, value.GetBuffer(255), 255+1);
}
value.ReleaseBuffer();
}

//设置ComboBox的文字
void SetComboBoxString(HWND hWndCtrl, const CString& value)
{
// set current selection based on model string
OutputDebugString("SetComboBoxString:" + value);
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);
}
}

---------------------------------------------------------------

你的Combox是允许编辑的,默认是-1,你输入一个D进去,GetCurSel的时候,当然也是-1,因为你并没有选什么。

CString strInput;
int nSel=m_ComboBox.GetCurSel();
m_ComboBox.GetLBText(nSel,strInput);
这样,就可以得到你选中的了,在strInput里面
---------------------------------------------------------------

if(m_ComboBox.SelectString(m_combo) == CB_ERR)
{
这个String m_combo在原来的list中没有.
}
---------------------------------------------------------------

COMBOBOX没有提供这种“在edit中输入内容,回车后就自动把内容添加到combobox下拉列表中”的功能

这种功能需要自己实现。。。

在pretranslatemessage中,检测到回车,即添加combobox中内容到下来列表中

BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
CString str;
if( pMsg->message == WM_KEYDOWN )
{
switch( pMsg->wParam )
{
case VK_RETURN:
CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
if(pMsg->hwnd == pEdit->m_hWnd )
{
m_combo1.GetWindowText(str);
m_combo1.AddString(str);
}
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: