自己自定义了一个CComboBox的子控件,不知道哪里出问题了,求高手帮忙看一下。

twiker 2011-08-17 08:45:44
我从CComboBox派生了一个子控件,目的是在输入的时候能够列举相似的选项,并以下拉菜单的方式显示出来,程序响应CBN_EDITCHANGE消息,在输入的时候查找一个set中的数据,将所有相似的字符串添加到CComboBox中并弹出菜单,但是不知道为啥我在类的访问到的set都是空。请大神们帮忙看看。我已经想了半天了。

类申明

typedef set< string > str_set;

/////////////////////////////////////////////////////////////////////////////
// CPopupEdit window

class CPopupEdit : public CComboBox
{
// Construction
public:
    CPopupEdit();

// Attributes
public:
    afx_msg void OnEditChange();
    virtual ~CPopupEdit();

public:
    BOOL PreTranslateMessage(MSG* pMsg);
    void SetPopupEditObserver(CPopupEditObserver* aObserver);
    void AddPopupOption(string str);

protected:
void UpdatePopupMenu();

protected:
    DECLARE_MESSAGE_MAP()

protected:
    str_set m_strsDataSource;
    CPopupEditObserver* m_peoObserver;
};




CPopupEdit::CPopupEdit()
:m_peoObserver(NULL)
{
    AddPopupOption(string("hello"));
    AddPopupOption(string("world"));
    AddPopupOption(string("FaultCodeGeneratorCC"));
}


CPopupEdit::~CPopupEdit()
{
}


BEGIN_MESSAGE_MAP(CPopupEdit, CComboBox)
    //{{AFX_MSG_MAP(CPopupEdit)
        // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPopupEdit message handlers

class string_finder
{
public:
    string_finder(string& str):m_str(str){}
    bool operator()(string str)
    {
        return m_str.find(str)==0;
    }
protected:
private:
    string m_str;
};


void CPopupEdit::SetPopupEditObserver(CPopupEditObserver* aObserver)
{
    m_peoObserver=aObserver;
}

void CPopupEdit::AddPopupOption(string str)
{
    m_strsDataSource.insert(str);
}

void CPopupEdit::UpdatePopupMenu()
{
    if (0==m_strsDataSource.size()) {
        return;
    }
    CString str;
    GetWindowText(str);
    if (str=="") {
        return;
    }

    str_set::iterator itor=find_if(m_strsDataSource.begin(),m_strsDataSource.end(),string_finder(string(str.GetBuffer(str.GetLength()))));

    if (itor==m_strsDataSource.end())
    {
        this->ShowDropDown(FALSE);
        return;//not found
    }
    
    this->ResetContent();

    for (int ii=0; itor!=m_strsDataSource.end(); ++itor, ++ii) {
        this->InsertString(ii,itor->c_str());
    }
    
    this->ShowDropDown(TRUE);
}

BOOL CPopupEdit::PreTranslateMessage(MSG* pMsg)
{
    switch(pMsg->message)
    {
    case WM_KEYDOWN:
        {
            switch(pMsg->wParam)
            {
            case VK_RETURN:
                if(m_peoObserver)
                    m_peoObserver->HandleInputCompleted();
                break;

            default:
                break;
            }
        }
        break;

    default:
        break;
    }

    return CComboBox::PreTranslateMessage(pMsg);
}

void CPopupEdit::OnEditChange()
{
    UpdatePopupMenu();
}
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
twiker 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chunyou128 的回复:]

C/C++ code
// MFC Collection class search
CJStruct * pCurrent;
for (int nCol=-1;TotalCol>nCol;nCol++)
{

if (nStartAfter == -1) //search for entire combobox
{
……
[/Quote]
我实在看不出你的代码跟我的问题有任何关系,求点拨
twiker 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chunyou128 的回复:]

C/C++ code
// MFC Collection class search
CJStruct * pCurrent;
for (int nCol=-1;TotalCol>nCol;nCol++)
{

if (nStartAfter == -1) //search for entire combobox
{
……
[/Quote]
我实在看不出你的代码跟我的问题有任何关系,求点拨
chunyou128 2011-08-17
  • 打赏
  • 举报
回复
// MFC Collection class search
CJStruct * pCurrent;
for (int nCol=-1;TotalCol>nCol;nCol++)
{

if (nStartAfter == -1) //search for entire combobox
{
POSITION pos = m_jcbList.GetHeadPosition();
while (pos != NULL)
{
if (lpszString == (pCurrent = m_jcbList.GetNext(pos))->m_strKey[nCol+1])
return CComboBox::SetCurSel(pCurrent->m_nKey);

}

}
else
{ //if you don't want to search the entire Combobox
POSITION pos = m_jcbList.GetHeadPosition();
while (pos != NULL)
{

if (nStartAfter == (pCurrent = m_jcbList.GetNext(pos))->m_nKey)
{
if (lpszString == pCurrent->m_strKey[nCol+1])
return CComboBox::SetCurSel(pCurrent->m_nKey);
break;
}

}
} //END ELSE

} //end for
twiker 2011-08-17
  • 打赏
  • 举报
回复
就是到DataSource.size()=0那就返回了
Eleven 2011-08-17
  • 打赏
  • 举报
回复
你Debug下调试一下UpdatePopupMenu函数

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧