CheckedListBox扩展方法代码

本文提供了一组实用的扩展方法,用于简化对CheckedListBox控件的操作,包括全选、取消全选、反选以及根据列表设置选择状态等功能。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1public static class CheckedListBox扩展
 2ExpandedBlockStart.gifContractedBlock.gif{
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 4    /// 全部选定所有项
 5    /// </summary>

 6    public static void 全部选定(this CheckedListBox c)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 8        for (int i = 0; i < c.Items.Count; i++)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        {
10            c.SetItemChecked(i, true);
11        }

12    }

13
14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
15    /// 全部取消选定所有项
16    /// </summary>

17    public static void 全部取消选定(this CheckedListBox c)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    {
19        for (int i = 0; i < c.Items.Count; i++)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
21            c.SetItemChecked(i, false);
22        }

23    }

24
25ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
26    /// 反向选定所有项
27    /// </summary>

28    public static void 反向选定(this CheckedListBox c)
29ExpandedSubBlockStart.gifContractedSubBlock.gif    {
30        for (int i = 0; i < c.Items.Count; i++)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
32            c.SetItemChecked(i, !c.GetItemChecked(i));
33        }

34    }

35
36ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
37    /// 根据选定状态列表中的值,逐一设定各列表项的选定状态
38    /// </summary>
39    /// <param name="选定状态列表">包含所有列表项对应的选定状态的列表</param>

40    public static void 自设选定(this CheckedListBox c, IEnumerable<bool> 选定状态列表)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    {
42        int x = 0;
43        foreach (bool f in 选定状态列表)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        {
45            c.SetItemChecked(x++, f);
46        }

47    }

48
49ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
50    /// 根据选定项索引列表的值,设定指定索引处列表项的选定状态为已选定,其它处均设为未选定
51    /// </summary>
52    /// <param name="选定项索引列表">包含选定列表项的索引位置的列表</param>

53    public static void 自设选定(this CheckedListBox c, IEnumerable<int> 选定项索引列表)
54ExpandedSubBlockStart.gifContractedSubBlock.gif    {
55        c.全部取消选定();
56        foreach (int f in 选定项索引列表)
57ExpandedSubBlockStart.gifContractedSubBlock.gif        {
58            c.SetItemChecked(f, true);
59        }

60    }

61
62ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
63    /// 将一个字典作为数据源加载到CheckedListBox,字典的键即为列表项的值,字典的值用以指示列表项是否被选定
64    /// </summary>
65    /// <typeparam name="类型">自定义类型</typeparam>
66    /// <param name="数据源">数据源</param>

67    public static void 数据源设定<类型>(this CheckedListBox c, Dictionary<类型, bool> 数据源)
68ExpandedSubBlockStart.gifContractedSubBlock.gif    {
69        var l=数据源.Values.ToArray();
70        c.DataSource = null;
71        c.DataSource = 数据源.Keys.ToList();
72        c.自设选定(数据源.Values);
73    }

74
75ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
76    /// 将CheckedListBox的列表项及其选定状态作为字典返回,字典的键即为列表项的值,字典的值用以指示列表项是否被选定
77    /// </summary>
78    /// <typeparam name="类型">自定义类型</typeparam>
79    /// <returns>字典</returns>

80    public static Dictionary<类型, bool> 数据源获取<类型>(this CheckedListBox c)
81ExpandedSubBlockStart.gifContractedSubBlock.gif    {
82        var l = new Dictionary<类型, bool>();
83        for (int i = 0; i < c.Items.Count; i++)
84ExpandedSubBlockStart.gifContractedSubBlock.gif        {
85            l.Add((类型)c.Items[i], c.GetItemChecked(i));
86        }

87        return l;
88    }

89}

 

该扩展提供了控制、加载、导出CheckedListBox选定状态的一些实用方法。

 

用于测试加载及导出的代码片段:

ContractedBlock.gif ExpandedBlockStart.gif Code
private void button2_Click(object sender, EventArgs e)
{
    var l 
= new Dictionary<stringbool>();
    l.Add(
"a"true);
    l.Add(
"q"false);
    l.Add(
"w"true);
    l.Add(
"e"false);
    checkedListBox1.数据源设定
<string>(l);
}

private void button3_Click(object sender, EventArgs e)
{
    checkedListBox2.数据源设定
<string>(checkedListBox1.数据源获取<string>());
}

 

下载代码片段

下载本文的PDF版本


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值