您的位置:首页 > 其它

CComboBox的SetItemData和GetItemData

2013-04-27 16:38 429 查看
在做项目时,发现GetItemData老是返回null,很是不明白,后来经过研究发现时自己SetItemData时有些问题。所以把它记录下来,以防下次再错,也可以给新手一个提醒。

我有一个CComboBox的成员m_Machine,以下是错误的情况

m_Machine.ResetContent();

DB::CRecordSet *set = (DB::CRecordSet*)wParam;

for ( int i = 0; i < lParam; i++ )
{
DB::CRecordRow Row = set->GetNextRow();
ASSERT(Row.IsValid());
m_Machine.AddString(Row[0]);
TCHAR* p = new TCHAR[48];
StringCchCopy(p,48,Row[1]);
m_Machine.SetItemData(i,(DWORD_PTR)p);

TRACE("%s - %s\n",Row[0],p);
}


在数据库中查询数据插入到m_Machine中,然后使用以下的代码获取

idx=m_Machine.GetCurSel();
if(idx<0){
AfxMessageBox(_T("没有选择机器!"),MB_ICONERROR);
return;
}
m_Machine.GetLBText(idx,m_PosInfo.machine);
TCHAR *pX = (TCHAR*)m_Machine.GetItemData(idx);


发现pX总是null。

改成以下代码就可以了。

m_Machine.ResetContent();

DB::CRecordSet *set = (DB::CRecordSet*)wParam;

for ( int i = 0; i < lParam; i++ )
{
DB::CRecordRow Row = set->GetNextRow();
ASSERT(Row.IsValid());
int ix=m_Machine.AddString(Row[0]);
TCHAR* p = new TCHAR[48];
StringCchCopy(p,48,Row[1]);
m_Machine.SetItemData(ix,(DWORD_PTR)p);

TRACE("%s - %s\n",Row[0],p);
}


不能直接使用i作为SetItemData的索引,而应该通过AddString返回的值作为插入的索引。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: