VC CListBox用法总结

CListBox添加项,得到选中的单项或多项的值。

OnInitDialog();函数里初始化

// TODO: 在此添加额外的初始化代码
CListBox *pCtrl = (CListBox *)GetDlgItem(IDC_LIST1);
pCtrl
->AddString(_T("A"));
pCtrl
->AddString(_T("B"));

 对CListBox的操作

大气象
void CUseControlDlg::OnBnClickedButtonOk()
{
    
// TODO: 在此添加控件通知处理程序代码
    CListBox *m_lstInfo = (CListBox *)GetDlgItem(IDC_LIST1);
    
    
//那么你可以用一个循环取出里面的值:
    /*
    CString str; //临时变量用来接收项的字符串
    CString strAll=_T(""); //所有项
    int nCount = m_lstInfo->GetCount();//得到项目总数
    for(int i = 0; i< nCount; ++i)
    {
        m_lstInfo->GetText(i,str);
        strAll = strAll + str + _T("\r\n");
    }
    AfxMessageBox(strAll);
    
*/

    
//取出单选选中的值
    /*
    int index;
    CString selectStr;
    index = m_lstInfo->GetCurSel();
    m_lstInfo->GetText(index,selectStr);
    AfxMessageBox(selectStr);
    
*/

    
//多选,设置selection为Multiple
    int nCount = m_lstInfo->GetSelCount();
    CString cCount;
    CArray
<int,int> aryListBoxSel;

    aryListBoxSel.SetSize(nCount);
    m_lstInfo
->GetSelItems(nCount, aryListBoxSel.GetData()); 
    
//得到总数
    cCount.Format(_T("%d"),nCount);
    AfxMessageBox(cCount);
    
//得到选中的多项
    for (int i=0;i<nCount;i++)
    {
        CString selStr;
        m_lstInfo
->GetText(aryListBoxSel[i],selStr);
        AfxMessageBox(selStr);
    }
天祺围棋:www.tianqiweiqi.com呵呵

凡事以大气象去面对,优秀是一种习惯。

原文地址:https://www.cnblogs.com/greatverve/p/clistbox.html