常用属性:
CheckOnClick:true那么单击条目就能将条目勾选;如果为false,则要双击条目才能将其勾选。
MultiColumn:指示是否开启多列显示条目。该属性是配合ColumnWidth属性一起使用。
Count:该属性表示列表中条目的总量。用法如下:
SelectedItem:获取选中的条目
SelectedItems:属性是一个数组,保存着被选中的条目的集合,可通过下标来获取条目
CheckedItems[i]: 获取列表中的项
【实例】
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CheckedListBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /* *获取列表中的项使用的是 Checkedltems 属性 *获取当前选中的文本使用的是 Selectedltem 属性。 */ private void button1_Click(object sender, EventArgs e) { string msg = ""; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { msg = msg + " " + checkedListBox1.SelectedItem.ToString(); // msg = msg + " " + checkedListBox1.CheckedItems[i].ToString(); } if (msg != "") { MessageBox.Show("您购买的水果有:" + msg, "提示"); } else { MessageBox.Show("您没有选购水果!", "提示"); } } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace CheckedListBox { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
文章来源: https://blog.csdn.net/zs1342084776/article/details/91450471