ListBox控件基本操作

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; 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值