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


得到/设置DropDown ComboBox的文字


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

              

        在使用DropDown类型的ComboBox时,如果使用者输入非ComboBox中列出的值,那么ComboBox的GetCurSel()的值是CB_ERR,相关的GetLBText()和GetLBTextLen()因为没有index也就无法工作。

在DDX时,可以把ComboBox和CString对应起来,采用它的原理,就可以自己来得到或设置DropDown ComboBox的文字了。

//得到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);
    }
}


关于我们 版权声明 广告服务 联系我们 友情链接 加入收藏
站长:施昌权    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号