CheckBoxList扩展方法代码

public static class CheckedListBox扩展

{

    ///

    /// 全部选定所有项

    ///

    public static void 全部选定(this CheckedListBox c)

    {

        for (int i = 0; i < c.Items.Count; i++)

        {

            c.SetItemChecked(i, true);

        }

    }

 

    ///

    /// 全部取消选定所有项

    ///

    public static void 全部取消选定(this CheckedListBox c)

    {

        for (int i = 0; i < c.Items.Count; i++)

        {

            c.SetItemChecked(i, false);

        }

    }

 

    ///

    /// 反向选定所有项

    ///

    public static void 反向选定(this CheckedListBox c)

    {

        for (int i = 0; i < c.Items.Count; i++)

        {

            c.SetItemChecked(i, !c.GetItemChecked(i));

        }

    }

 

    ///

    /// 根据选定状态列表中的值,逐一设定各列表项的选定状态

    ///

    /// 选定状态列表">包含所有列表项对应的选定状态的列表

    public static void 自设选定(this CheckedListBox c, IEnumerable<bool> 选定状态列表)

    {

        int x = 0;

        foreach (bool f in 选定状态列表)

        {

            c.SetItemChecked(x++, f);

        }

    }

 

    ///

    /// 根据选定项索引列表的值,设定指定索引处列表项的选定状态为已选定,其它处均设为未选定

    ///

    /// 选定项索引列表">包含选定列表项的索引位置的列表

    public static void 自设选定(this CheckedListBox c, IEnumerable<int> 选定项索引列表)

    {

        c.全部取消选定();

        foreach (int f in 选定项索引列表)

        {

            c.SetItemChecked(f, true);

        }

    }

 

    ///

    /// 将一个字典作为数据源加载到CheckedListBox,字典的键即为列表项的值,字典的值用以指示列表项是否被选定

    ///

    /// 类型">自定义类型

    /// 数据源">数据源

    public static void 数据源设定<类型>(this CheckedListBox c, Dictionary<类型, bool> 数据源)

    {

        var l=数据源.Values.ToArray();

        c.数据源设定(数据源.Keys.ToList());

        c.自设选定(数据源.Values);

    }

 

    ///

    /// CheckedListBox的列表项及其选定状态作为字典返回,字典的键即为列表项的值,字典的值用以指示列表项是否被选定

    ///

    /// 类型">自定义类型

    /// 字典

    public static Dictionary<类型, bool> 数据源获取<类型>(this CheckedListBox c)

    {

        var l = new Dictionary<类型, bool>();

        for (int i = 0; i < c.Items.Count; i++)

        {

            l.Add((类型)c.Items[i], c.GetItemChecked(i));

        }

        return l;

    }

}

转载于:http://blog.itpub.net/10474909/viewspace-592662/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值