常用属性:
CheckOnClick:true那么单击条目就能将条目勾选;如果为false,则要双击条目才能将其勾选。
MultiColumn:指示是否开启多列显示条目。该属性是配合ColumnWidth属性一起使用。
SelectionMode: One None
Sorted: 如果为true,则条目会根据字母进行排序,如果为false,则不进行排序。
Items: 获取列表中条目的集合,通过下标获取指定条目。
object item=checkedListBox1.Items[i];
Count:该属性表示列表中条目的总量。用法如下:
int conut=checkedListBox1.Items.Count
SelectedItem:获取选中的条目
object item =checkedListBox1.SelectedItem;
SelectedItems:属性是一个数组,保存着被选中的条目的集合,可通过下标来获取条目
object item=checkedListBox1.SelectedItems[i];
checkedListBox1.Items.Add(item);
CheckedItems[i]: 获取列表中的项
checkedListBox1.CheckedItems[i].ToString();
【实例】
使用复选列表框完成选购水果的操作。
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());
}
}
}
Checkedltems效果:

Selectedltem效果:

本文介绍了C#中的CheckListBox控件,详细讲解了其常用属性,包括CheckOnClick、MultiColumn、SelectionMode、Sorted等,并展示了如何操作Items、Count、SelectedItem和SelectedItems。此外,还提供了一个实例,演示了如何利用该控件实现选购水果的操作。

2702

被折叠的 条评论
为什么被折叠?



