MFC ListBox(列表框)的使用
1添加數(shù)據(jù)
聲明控件變量的類別為Control,變量類型為CListBox,變量名為m_ListBox_Content.
m_ListBox_Content.AddString(_T("123"));
m_ListBox_Content.AddString(_T("漢字"));
m_ListBox_Content.AddString(_T("English"));
m_ListBox_Content.AddString(_T("!@#$%^&*()"));
2獲取數(shù)據(jù)
CString s;
m_ListBox_Content.GetText(1,s);
MessageBox(s,_T("取得第2行數(shù)據(jù)"),MB_OK);
s.ReleaseBuffer();
將會(huì)得到"漢字"這個(gè)字符串,如果沒(méi)有得到"漢字"這個(gè)字符串,是因?yàn)?/span>ListBox的Sort屬性設(shè)為True了.設(shè)為False之后就按照你編寫(xiě)的順序?qū)懭?/span>.
3獲取選擇的數(shù)據(jù)
首先要將ListBox的Selection屬性設(shè)置為Multiple;
int nSel;
nSel=m_ListBox_Content.GetCurSel();
CString s;
m_ListBox_Content.GetText(nSel,s);
MessageBox(s,_T("您選擇的是"),MB_OK);
s.ReleaseBuffer();
4獲取選擇ListBox項(xiàng)的多個(gè)數(shù)據(jù)
首先要將ListBox的Selection的屬性設(shè)置為Multiple
int nSel = m_ListBox_Content.GetSelCount();
CArray< int,int& > arrayListSel;
arrayListSel.SetSize(nSel);
m_ListBox_Content.GetSelItems(nSel,arrayListSel.GetData());
CString s = _T("");
for( int i=0; i< nSel; i++ )
{
m_ListBox_Content.GetText( arrayListSel[i], s);
MessageBox(s,_T("您選擇的是"),MB_OK);
}
5雙擊刪除所選項(xiàng)
添加一個(gè)ListBox的雙擊事件
m_ListBox_Content.DeleteString(m_ListBox_Content.GetCurSel());