MFC ListBox(列表框)的使用
1添加數據
聲明控件變量的類別為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獲取數據
CString s;
m_ListBox_Content.GetText(1,s);
MessageBox(s,_T("取得第2行數據"),MB_OK);
s.ReleaseBuffer();
將會得到"漢字"這個字符串,如果沒有得到"漢字"這個字符串,是因為ListBox的Sort屬性設為True了.設為False之后就按照你編寫的順序寫入.
3獲取選擇的數據
首先要將ListBox的Selection屬性設置為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項的多個數據
首先要將ListBox的Selection的屬性設置為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雙擊刪除所選項
添加一個ListBox的雙擊事件
m_ListBox_Content.DeleteString(m_ListBox_Content.GetCurSel());