首    页 界面/窗口 网络/通讯 数据库 组件开发 图像/多媒体 NET/Web 其它技术 源码下载 资料下载 软件共享 软件外包 曲艺杂谈
栏目导航:  首    页  |  界面/窗口  |  组合框   


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


原作者:qsdnet    源出处:CSDN    发布者:施昌权    发布类型:转载    发布日期:2008-10-15

              

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);  
}

关于我们 版权声明 广告服务 联系我们 友情链接 加入收藏
站长:施昌权    Email:scq2099yt@163.com    MSN:scq2099yt@live.cn    QQ:14046300    本站QQ群:67202409
Copyright © 2008     卓为VC(www.joyvc.cn)    All Rights Reserved    建议分辨率 1024×768
本站由施昌权制作维护
京ICP备09012297号
>
京ICP备09012297号