Windows图形界面(GUI)-MFC-C/C++ - 列表框(ListBox) - CListBox

目录

列表框(ListBox) - CListBox

基本概念

成员方法

示例代码


列表框(ListBox) - CListBox

基本概念

  • 列表框控件是一个窗口,通常在对话框中使用,用于显示一个项目列表,用户可以从中选择一个或多个项目。

  • 列表框可以设置为单选(一次只能选择一个项目)或多选(可以同时选择多个项目)。

  • 在MFC中,列表框控件由 CListBox 类管理。这个类提供了操作列表框的方法和属性。

成员方法

  • Create:创建列表框控件并将其附加到 CListBox 对象。

  • AddString:向列表框中添加一个字符串。

  • DeleteString:从列表框中删除一个字符串。

  • InsertString:在列表框中的指定位置插入一个字符串。

  • ResetContent:清除列表框中的所有字符串。

  • GetCount:获取列表框中字符串的数量。

  • GetCurSel:获取列表框中当前选中项的索引(单选模式)。

  • SetCurSel:设置列表框中当前选中的字符串。

  • GetSel:获取列表框中指定项的选中状态。

  • SetSel:设置列表框中指定项的选中状态(多选模式)。

  • GetText:从列表框中获取指定项的文本。

  • GetTextLen:获取列表框中指定项的文本长度。

示例代码
void CMainDlg::OnBnClickedButton4()
{
	CString strListItem;
	m_ListItem.GetWindowTextW(strListItem);
	m_ListBox.AddString(strListItem);
}

void CMainDlg::OnBnClickedButton5()
{
	int nIndex = m_ListBox.GetCurSel();
	if (nIndex != LB_ERR)
	{
		m_ListBox.DeleteString(nIndex);
	}
}

void CMainDlg::OnBnClickedButton6()
{
	int nIndex = m_ListBox.GetCurSel();
	if (nIndex != LB_ERR)
	{
		CString strListItem;
		m_ListItem.GetWindowTextW(strListItem);
		m_ListBox.InsertString(nIndex, strListItem);
	}
}

void CMainDlg::OnBnClickedButton15()
{
	m_ListBox.ResetContent();
}

void CMainDlg::OnBnClickedButton16()
{
	m_ListBox.SetCurSel(0);
}

void CMainDlg::OnBnClickedButton17()
{
	int nIndex = m_ListBox.GetCurSel();
	if (nIndex != LB_ERR)
	{
		CString strItemText;
		m_ListBox.GetText(nIndex, strItemText);
		AfxMessageBox(strItemText);
	}
}

void CMainDlg::OnLbnDblclkList1()
{
	int nIndex = m_ListBox.GetCurSel();
	if (nIndex != LB_ERR)
	{
		CString strItemText;
		m_ListBox.GetText(nIndex, strItemText);
		AfxMessageBox(strItemText);
	}

	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值