以下是源代码
1)CListBox* g_pListRecord;
2)g_pListRecord = (CListBox *)GetDlgItem(IDC_LIST_Record);
3)
int DBConnection::ReadItemInfo()
{
g_tLog.print("readListbox fo ItemInfo \n");
long nRecordId = 0;
if (m_bDBOpened == FALSE)
m_bDBOpened=OpenDB();
if (m_bDBOpened == FALSE)
{
g_tLog.print("OpenDB fail.\n");
return -1;
}
long nRet=0;
char sql[1024];
sprintf(sql,"select * from HOS_ITEM");
try
{
_CommandPtr pCmd;
pCmd.CreateInstance(__uuidof(Command));
pCmd->ActiveConnection = m_pConn;
pCmd->CommandText =_bstr_t(sql);
pCmd->Parameters->Refresh();
pCmd->CommandType = adCmdText;
_RecordsetPtr pRs=pCmd->Execute(NULL,NULL,adCmdText);
if (pRs->GetState() == adStateClosed ||pRs->adoEOF)
return -1;
int nIndex=0;
ItemInfo *item;
while (pRs->GetState() != adStateClosed && !pRs->adoEOF)//
{
char ID[256],Name[256];
CString strTemp="";
_variant_t var;
//ID
var.Clear();
strTemp.Empty();
var=(LPTSTR)_bstr_t(pRs->GetCollect(L"ID"));
if (var.vt!=VT_NULL&&var.vt!=VT_EMPTY)
{
strTemp=(LPTSTR)_bstr_t(var);
strTemp.TrimLeft();
strTemp.TrimRight();
}
lstrcpy(ID, strTemp);
//Name
var.Clear();
strTemp.Empty();
var=(LPTSTR)_bstr_t(pRs->GetCollect(L"Name"));
if (var.vt!=VT_NULL&&var.vt!=VT_EMPTY)
{
strTemp=(LPTSTR)_bstr_t(var);
strTemp.TrimLeft();
strTemp.TrimRight();
}
lstrcpy(Name, strTemp);
char result[1024];
sprintf(result,"%20s%20s",ID,Name);
item=new ItemInfo;
item->ID=atol(ID);
strcpy(item->Name,Name);
g_tLog.print("ID[%d]Name[%s]\n",item->ID,item->Name);
g_pListItemInfo->SetItemData(nIndex,(DWORD_PTR)item);
nIndex++;
pRs->MoveNext();
}
pRs->Close();
pRs.Release();
}
catch (_com_error& e)
{
CloseDB();
g_tLog.print("com_error:%s,%s\n",(LPTSTR)e.Description(),sql);
return -1;
}
catch (...)
{
CloseDB();
g_tLog.print("error:%s,%s\n","出错,关闭数据库",sql);
return -1;
}
return nRecordId;//nRet;
}
其中要注意点:int nIndex=0; 必须从0开始才有效,记得在循环体里面加nIndex++;
其次, g_pListItemInfo->AddString(result);后再g_pListItemInfo->SetItemData(nIndex,(DWORD_PTR)item);
最后取数据:ItemInfo *item=(ItemInfo*)g_pListItemInfo->GetItemData(nIndex);g_tLog.print("选中id[%d]\n",item->ID);
分享到:
相关推荐
CListBox透明 透明CListBox 背景透明CListBox,非常好用,能编的过。
带图标的ClistBox带图标的ClistBox带图标的ClistBox
VS2010 CListBox改变指定行的颜色
VC中为CListBox控件加背景色,字体颜色的例子;对CListBox进行继承重载!
继承CListBox,可以执行拖拽。拖拽到grid control
MFC中CListBox类,VC++编程必备。
下载吧! 绝对好的入门代码!...涉及内容: CListCtrl 与 CListBox 的基本操作,比如CListCtrl列移到CListBos,删除CListBox, 将CListCtrl导出至本地文件,再从本地文件导入至CListCtrl 对CListBox用qsort排序!
使VC中CListBox带有复选框使VC中CListBox带有复选框
这个主要是用vc开发的一个文本颜色的CListBox控件, 添加一个颜色处理类,并继承CListBox,然后添加颜色控件的变量,在addString(文本,rgb),就完成了,就这么简单,代码运行可以看到结果!
本工程实例是一个完美的CListBox重写实例,作者经过封装将所有开发需要的CListBox功能都实现了,只要大家大胆拿来用就行了。使用之前要看看在CListBoxEBX.h文件开始的英文说明。包含的功能如下:增加一项、插入一项...
在网上看到很多人搜clistbox使用,本人特准备了该文档,介绍clistbox基本操作和使用情况
vs2005中 CListBox类常见函数用法(很全)
可以编辑的CListBox控件,源代码,直接加入工程使用
CListBox类常见函数用法
以重载方式响应CListBox右键,为回答网友问题的资源,不知道为什么上一次没传上去。
完美的CListBox控件,MFC控件,VC6
CHorzListBox类继承自CListBox, 可以根据内容自动显示调整水平滚动条
CListBox 支持文件拖拽
本资源是自画clistbox控件,实现在每行的左面显示图标右面显示文字,类似qq的好友列表
很漂亮的一款列表CListBox,可以直接调用,修改