[ 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);
}
---------------------------------------------------------------
//得到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);
}
相关文章推荐
- vc中关于SetItemData和GetItemData与对象相关联
- 关于RN中FlatList的一些东西,以及FlatList为什么会得到多个只显示几个信息的原因
- android 向JS传递数组为什么得到的不是数组呢?
- 关于语句是否能改变变量的值
- vc 动态控件的变量关联
- 关于silverlight的combobox选择项改变的问题
- AngularJS 为什么 我在controller里修改了 变量的值,dom 没有改变
- Matlab 通过一个方程得到关于某个变量的函数
- 关于win7里边使用telnet命令为什么提示telnet不是内部或外部命令,也不是可运行的程序或批处理文件的解决办法
- keil 4.22 调试的时候,watch中添加的变量为什么不是实时更新的?(解决了)
- 关于C语言中为什么getchar的返回类型不是char而是int的说明
- 关于为什么NSShadow的shadowColor属性是id类型而不是UIColor类型
- 解决用request.getRemoteHost()为什么得到的不是机器名,而是IP地址
- 关于 html append 为什么不是子级关系 $("#"+)
- 为什么Linux下的环境变量要用大写而不是小写
- VC 全局变量为什么还需要 extern
- MFC 关联变量 EditControl控件 UpdateData CString与double转换
- 关于VC单选按钮不能设置变量以及Group属性的设置问题
- ref C#中的ShowDialog和Show的区别 为什么再ShowDialog中修改变量时原窗口中的变量不会被改变 out如何在这里应用
- 为什么要使用事件而不是委托变量?