+ -
当前位置:首页 → 问答吧 → CComboBox取消当前的选择或者是当前选择无效

CComboBox取消当前的选择或者是当前选择无效

时间:2011-12-15

来源:互联网

CComboBox控制,DropList
当选择新的值后,会显示新的值。

如果这样,在选择新值的时候,先判断当前值是否可选或者说是当前值是否应该选,如果否,则保持上一次的值,即当前值 无效。如:
1、当前CComboBox的值为:北京;
2、下拉,选择新的值为:上海;
3、判断当前不应该选择:上海;
4、取消当前的选择,CComboBox的值还是:北京。

问:

有什么好的办法可以取消当前选择的值,使用CComboBox的值仍旧为:北京?谢谢

除去这种办法:
1、保存上一次选择的Index
2、取消的话重新设置上一次的Index

作者: lirg8405   发布时间: 2011-12-15

C/C++ code

//
int    CCBlistDlg::ComboItemFromPoint(CPoint pt,BOOL &bOutside)
{
    CRect rc;
    m_Combo.GetClientRect(&rc);
    int editHeight=rc.Height();
    m_Combo.GetDroppedControlRect(&rc);// include edit
//
    int n1stIdx=m_Combo.GetTopIndex();
    int listHeight=m_Combo.GetItemHeight(n1stIdx);
//
    bOutside=TRUE;
    int Result=-1;
//
    CRect itemRc;
    itemRc=rc;
    itemRc.top += editHeight;
    itemRc.bottom =itemRc.top + editHeight;
//
    int i=n1stIdx;
    while(itemRc.bottom < rc.bottom )
    {
        if(itemRc.PtInRect(pt))
        {
            Result=i;
            bOutside=FALSE;
            return Result;
        }
        itemRc.top += listHeight;
        itemRc.bottom += listHeight;
        i++;
    }
    return Result;
}
//
BOOL CCBlistDlg::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class
static int nID=-1;
    int newID;
    BOOL bOutside;
    if(pMsg->message==WM_LBUTTONUP)//WM_MOUSEMOVE)
    {
        if(m_Combo.GetDroppedState())
        {
            newID=ComboItemFromPoint(pMsg->pt,bOutside);
            if((!bOutside) && (nID != newID))
            {// once
                nID=newID;
                CString str;
                m_Combo.GetLBText(nID,str);
                afxDump << str << "\n";
                if(str=="5") return TRUE;
            }
        }
    }
    return CDialog::PreTranslateMessage(pMsg);
}


主要:combo list 中有1到15
if(str=="5") return TRUE;这个‘5’用户再也不能选择了!

作者: schlafenhamster   发布时间: 2011-12-15

热门下载

更多