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());

6. 列表框(ListBox)中的选项逐个往下移

// Select the next item of the currently selected one.

int nIndex = m_myListBox.GetCurSel();

int nCount = m_myListBox.GetCount();

if ((nIndex != LB_ERR) && (nCount > 1))

{

if (++nIndex < nCount)

m_myListBox.SetCurSel(nIndex);

else

m_myListBox.SetCurSel(0);

}

7.设置listbox的设置水平滚动条

// 设置listbox 的水平滚动条

CString str;

CSize sz;

int dx=0;

CDC *pDC = this->GetDC(); //find the longest string!

for (int i=0;i < m_AccessList.GetCount();i++)

{

m_AccessList.GetText( i, str );

sz = pDC->GetTextExtent(str);

if (sz.cx > dx)

dx = sz.cx;

}

this->ReleaseDC(pDC); //下面的代码屏蔽掉垂直滚动条所占的客户区的尺寸

CRect rectWindow,rectClient;

this->m_AccessList.GetClientRect(rectClient);

this->m_AccessList.GetWindowRect(rectWindow);

CSize extSize=rectWindow.Width()-rectClient.Width(); //设置水平滚动条的属性

this->m_AccessList.SetHorizontalExtent(dx+extSize.cx); // 设置listbox 的水平滚动条

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐