C# CheckBox/CheckedListBox 复选框/复选框列表 | 您所在的位置:网站首页 › 如何设置复选框选项按钮和标签内容 › C# CheckBox/CheckedListBox 复选框/复选框列表 |
目录 1 CheckBox获取选中项文本 2 CheckedListBox获取选中项文本 2.1 CheckedListBox常用属性 2.2 CheckedListBox常用方法 3 参考博客 1 CheckBox获取选中项文本(1)①创建一个winForm窗体应用,在设计窗体中拖入一个“Panel”面板容器,一个button,一个textBox(并将其Multiline属性设置为true)。②再在panel中拖入4个CheckBox控件并以此命名。如下图所示: (2) 双击“checkBox按钮”,进入click事件方法代码编写: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.textBox1.Clear(); if (this.cB1.Checked) { this.textBox1.AppendText(this.cB1.Text + "\n"); } if (this.cB2.Checked) { this.textBox1.AppendText(this.cB2.Text + "\n"); } if (this.cB3.Checked) { this.textBox1.AppendText(this.cB3.Text + "\n"); } if (this.cB4.Checked) { this.textBox1.AppendText(this.cB4.Text + "\n"); } } }选中几个复选框,点击按钮,效果如下: 该属性表示列表中条目的总量。用法如下: int conut=checkedListBox1.Items.Count 5Items获取列表中条目的集合,通过下标获取指定条目。 object item=checkedListBox1.Items[i]; 2.2 CheckedListBox常用方法具体参考:C#学习笔记:CheckedListBox控件的用法 2.3 CheckedListBox获取选中项文本同样的,在创建的winForm窗体中拖入CheckedListBox、textBox、button控件。如下所示: 双击“CheckedListBox按钮”,进入click事件方法代码编写,代码如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_CheckListBox_Click(object sender, EventArgs e) { //清空textBox内容 this.textBox2.Text = ""; //获取listBox项目总数 int N = this.checkedListBox1.Items.Count; //存储item名称的数组 string[] items = new string[N]; //注释部分为获取所有item并显示在textBox中 //for (int i = 0; i < N; i++) //{ // items[i] = this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]); // this.textBox2.AppendText(items[i]+"\n"); //} //获取选中的item并在textBox显示 for (int i = 0; i < N; i++) { //判断第i个项目是否选中 if (this.checkedListBox1.GetItemChecked(i)) { //获取当前项名称 string v = this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]); this.textBox2.AppendText(v + "\n"); } } } }结果如下图所示: 1 WinForm(C#)CheckedlistBox绑定数据,并获得选中的值(ValueMember)和显示文本(DisplayMember 2 C#学习笔记:CheckedListBox控件的用法 |
CopyRight 2018-2019 实验室设备网 版权所有 |