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

	
}
相关推荐
我是Superman丶8 分钟前
Arduino IDE 安装教程:Windows/macOS/Linux 开发环境搭建
ide·windows·macos
未来之窗软件服务15 分钟前
计算机二级[英文]-C 字符串移动—东方仙盟
c语言·开发语言·仙盟创梦ide·东方仙盟
Wang's Blog17 分钟前
Java框架快速入门: Spring Security+OAuth2之过滤器与过滤器链
java·开发语言·spring
谢尔登37 分钟前
01_Java基础速通
java·开发语言
前端炒粉37 分钟前
DOM 常用 API 速查表
开发语言·javascript·ecmascript
辻弋2011 小时前
自动检测硬件生成驱动清单、实时监控FPS、120秒性能记录——30+项优化模块化开关,所有改动可精确复原
服务器·windows·游戏引擎·电脑·开源软件
江湖人称菠萝包1 小时前
【Windows】《深入浅出Windows API程序设计:编程基础篇》笔记-Chapter8-子窗口控件
windows·笔记
小七在进步2 小时前
C++入门(2)
java·jvm·c++
en.en..2 小时前
C语言核心解析:#define与typedef本质区别
开发语言·c++·算法
2601_966949652 小时前
批量获取多只股票五档盘口的极简方案:QuantDash Python SDK 实战指南
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash