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/