ListBox控件单条记录操作
//设置ListBox控件属性:
// Styles -> Selection: Single
//关联ListBox控件变量:
CListBox m_pListRecord;
CString m_ListRecord;
CString m_strCurSelData; //定义变量存放获取的内容
//添加记录到ListBox控件
void Dlg::AddDataToListBox(CString strText)
{
m_pListRecord.AddString(strText);
}
//获取ListBox控件中选中的单条记录
bool Dlg::GetCurSelData(void)
{
int nSel = m_pListRecord.GetCurSel();
if(LB_ERR == nSel)
{
AfxMessageBox("请先选择一条记录!");
return false;
}
m_pListRecord.GetText(nSel, m_strCurSelData);
return true;
}
ListBox控件多条记录操作
//设置ListBox控件属性:
// Styles -> Selection: Multiple
//关联ListBox控件变量:
CListBox m_pListRecord;
CString m_ListRecord;
CStringArray m_strCurSelData; //定义变量存放获取的内容
int m_nMarkArrayCount; //定义变量,选择的记录个数
//添加记录到ListBox控件
void Dlg::AddDataToListBox(CString strText)
{
m_pListRecord.AddString(strText);
}
//获取ListBox控件中选中的多条记录
bool Dlg::GetCurSelData(void)
{
m_strCurSelData.RemoveAll();
m_nMarkArrayCount = m_pListRecord.GetSelCount(); //获取选择的行数
if(0 == m_nMarkArrayCount)
{
AfxMessageBox("请先至少选择一条记录!");
return false;
}
CArray<int, int> SelIndex;
SelIndex.SetSize(m_nMarkArrayCount);
m_pListRecord.GetSelItems(m_nMarkArrayCount, SelIndex.GetData()); //selindex数组为选择的多行的索引
CString m_SelData = "";
for(int i=0; i<m_nMarkArrayCount; ++i)
{
m_pListRecord.GetText(SelIndex.GetAt(i), m_SelData); //获取选择的记录
m_strCurSelData.Add(m_SelData);
}
return true;
}