【19】C# 窗体应用WinForm ——【列表框ListBox、复选列表框CheckedListBox】属性、方法、实例应用
文章目录
- 9 复选列表框CheckedListBox
- 10. 列表框ListBox
- 10.1 实例:买菜
- 10.2 实例:购菜 应用二
-
WinForm 是 Windows Form 的简称,是基于 .NET Framework 平台的客户端(PC软件)开发技术,是 C# 语言中的一个重要应用。
-
.NET 提供了大量 Windows 风格的控件和事件,可以直接拿来使用。
9 复选列表框CheckedListBox
复选列表框显示的效果与复选框类似,但在选择多个选项时操作比一般的复选框更方便。
目标:使用复选列表框完成选购水果的操作。
添加 CheckedListBox ;
在属性items中添加 复选列表框的内容;
在属性items中添加 复选列表框的内容,按确认
CheckedListBox中就添加了复选列表框
在button中添加函数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WinFormTest
{public partial class Form1 : Form{public Form1()// 修改窗体初始化函数{InitializeComponent();}private void button1_Click(object sender, EventArgs e){string msg = "";for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++){msg = msg + " " + checkedListBox1.CheckedItems[i].ToString();}if (msg != ""){MessageBox.Show("您购买的商品有:" + msg, "提示");}else{MessageBox.Show("您没有选购商品!", "提示");}}}
}
运行测试
10. 列表框ListBox
列表框控件中有一些属性与前面介绍的控件不同,如下表所示:
属性名 | 作用 |
---|---|
MultiColumn | 获取或设置列表框是否支持多列,如果设置为 True,则表示支持多列; 如果设置为 False,则表示不支持多列,默认为 False |
Items | 获取或设置列表框控件中的值 |
SelectedItems | 获取列表框中所有选中项的集合 |
SelectedItem | 获取列表框中当前选中的项 |
SelectedIndex | 获取列表框中当前选中项的索引,索引从 0 开始 |
SelectionMode | 获取或设置列表框中选择的模式,当值为 One 时,代表只能选中一项, 当值为 MultiSimple 时,代表能选择多项, 当值为 None 时,代表不能选 择,当值为 MultiExtended 时,代表能选择多项,但要在按下 Shift 键后 再选择列表框中的项 |
列表框还提供了一些方法来操作列表框中的选项,由于列表框中的选项是一个集合形式的,列表项的操作都是用 Items 属性进行的
。
10.1 实例:买菜
使用列表框列出所需的商品。
注: ListBox实现多选需要设置窗体的 SelectionMode 属性为 MultiSimple
。
添加 ListBox ;
在属性items中添加 复选列表框的内容;
青菜
黄瓜
南瓜
豌豆
辣椒
茄子
输入确认后
在button 中添加代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WinFormTest
{public partial class Form1 : Form{public Form1()// 修改窗体初始化函数{InitializeComponent();}private void button1_Click(object sender, EventArgs e){string msg = "";for (int i = 0; i < listBox1.SelectedItems.Count; i++){msg = msg + " " + listBox1.SelectedItems[i].ToString();}if (msg != ""){MessageBox.Show("您购买的商品有:" + msg, "提示");}else{MessageBox.Show("您没有选购商品!", "提示");}}}
}
注: ListBox实现多选需要设置窗体的 SelectionMode 属性为 MultiSimple
。
默认值为one
10.2 实例:购菜 应用二
- 在上述实例的基础上添加两个按钮,一个负责向列表框中添加菜品,一个负责删除选中的列表项。
上述实例的基础上添加 1个 label,1个TextBox,2个Button
修改属性:
Button2属性中Name:add,Text:添加
Button3属性中Name:Del,Text:删除
编辑代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WinFormTest
{public partial class Form1 : Form{public Form1()// 修改窗体初始化函数{InitializeComponent();}private void button1_Click(object sender, EventArgs e)//下单{string msg = "";for (int i = 0; i < listBox1.SelectedItems.Count; i++){msg = msg + " " + listBox1.SelectedItems[i].ToString();}if (msg != ""){MessageBox.Show("您选择的商品是:" + msg, "提示");}else{MessageBox.Show("您没有选择商品", "提示");}}private void add_Click(object sender, EventArgs e)//添加{// 当文本框中的值不为空时将其添加到列表框中if (textBox1.Text != ""){listBox1.Items.Add(textBox1.Text);}else{MessageBox.Show("请添加商品!");}}private void Del_Click(object sender, EventArgs e)//删除{//由于列表框控件中允许多选所以需要循环删除所有已选项int count = listBox1.SelectedItems.Count; // 获取listBox内被选中的元素数量List<string> itemValues = new List<string>(); // 定义一个字符串从列表if (count != 0){for (int i = 0; i < count; i++){itemValues.Add(listBox1.SelectedItems[i].ToString());}foreach (string item in itemValues){listBox1.Items.Remove(item);}}else{MessageBox.Show("请选择需要删除的商品!");}}}
}
运行测试
删除商品 辣椒
添加商品 鸡蛋