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

目录

[列表框(ListBox) - CListBox](#列表框(ListBox) - CListBox)

基本概念

成员方法

示例代码


列表框(ListBox) - CListBox

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

示例代码
cpp 复制代码
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);
	}

	
}
相关推荐
LiLiYuan.7 小时前
【字符串常量池】
java·开发语言·面试
瑞码空间7 小时前
Python爬虫进阶实战笔记
开发语言·python·计算机·python爬虫
16月6日-晴8 小时前
Java面向对象进阶—static
java·开发语言
爱跳舞的烤冷面8 小时前
自学嵌入式第16天(数据结构篇--链表进阶操作)
开发语言
持敬chijing8 小时前
PHP开发-环境搭建-安装phpstudy-vscode工具
开发语言·vscode·php
huainingning8 小时前
微软windows系统官网IPV6临时地址介绍
windows·microsoft·智能路由器
charlie1145141918 小时前
Cinux · 第一次跳进 Ring 3:用户态与特权隔离
开发语言·c++·操作系统·开源项目
别动我齐刘海8 小时前
机器学习基础2——C++、OpenCV、点云、Open3D
c++·人工智能·opencv·机器学习·计算机视觉·机器人·ros2
wuyk5558 小时前
1.栈:后进先出的线性数据结构
c语言·数据结构·stm32·单片机
躺不平的理查德9 小时前
Windows C++ 第三方库使用流程备忘录--OpenCV
开发语言·c++