本文提供了Checkbox与CheckedListBox、DataGridViewCheckBoxColumn等的联动关系

1、CheckboxAssociateFactroy.Create创建联动关系实例

     /// <summary>
/// Checkbox与其他集合之间进行关联
/// </summary>
public class CheckboxAssociateFactroy
{
public static CheckboxAssociation Create(Control checkBoxSideObj, object listSideObj)
{
ICheckboxSide checkBoxSide = null;
IListSide listSide = null; var checkBox = checkBoxSideObj as CheckBox;
if (checkBox != null)
checkBoxSide = new CheckBoxSide(checkBox);
var checkBoxX = checkBoxSideObj as CheckBoxX;
if (checkBoxX != null)
checkBoxSide = new CheckBoxXSide(checkBoxX); var checkBoxColumn = listSideObj as DataGridViewCheckBoxColumn;
if (checkBoxColumn != null)
listSide = new DataGridViewCheckBoxColumnSide(checkBoxColumn); var checkBoxXColumn = listSideObj as DataGridViewCheckBoxXColumn;
if (checkBoxXColumn != null)
listSide = new DataGridViewCheckBoxXColumnSide(checkBoxXColumn); var checkedListBox = listSideObj as CheckedListBox;
if (checkedListBox != null)
listSide = new CheckedListBoxSide(checkedListBox); var listBoxAdv = listSideObj as ListBoxAdv;
if (listBoxAdv != null)
listSide = new ListBoxAdvSide(listBoxAdv); if (checkBoxSide == null)
throw new ArgumentException($"Can not get an {nameof(ICheckboxSide)} from {nameof(checkBoxSideObj)}");
if (listSide == null)
throw new ArgumentException($"Can not get an {nameof(IListSide)} from {nameof(listSideObj)}"); return new CheckboxAssociation(checkBoxSide, listSide);
}
}

2、Checkbox侧的抽取的接口

     /// <summary>
/// The association of list side, such as Checkbox, CheckboxX(DotNetBar), eg
/// </summary>
public interface ICheckboxSide : IDisposable
{
/// <summary>
/// Get the Checked property value of Checkbox
/// </summary>
bool Checked { get; } /// <summary>
/// Notify others that my check property changed.
/// </summary>
Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of Checkbox
/// </summary>
/// <param name="checkState"></param>
void UpdateCheckedProperty(CheckState checkState);
}

3、集合列表如CheckedListBox/DataGridViewCheckBoxColumn等抽取的接口

     /// <summary>
/// The association of list side, such as DataGridViewCheckBoxColumn, ListBox, eg
/// </summary>
public interface IListSide : IDisposable
{
/// <summary>
/// Get the total of all items
/// </summary>
int ItemsTotal { get; } /// <summary>
/// Get count of checked items
/// </summary>
int CheckedCount { get; } /// <summary>
/// Notify others that same items check property changed.
/// </summary>
Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of all items
/// </summary>
/// <param name="setChecked"></param>
void UpdateCheckedProperty(bool setChecked);
}
}

4、联动关系类CheckboxAssociation,当使用Factory创建好联动关系的实例后,调用CheckboxAssociation.UpdateCheckboxSide()方法可以根据你代码设置好的CheckedListBox的勾选情况来更新Checkbox一侧,

调用UpdateListSide则可以根据Checkbox的勾选来全选或者全不选CheckedListBox控件中的数据

     /// <summary>
/// The association between CheckboxSide and ListSide
/// </summary>
public class CheckboxAssociation : IDisposable
{
private ICheckboxSide _checkboxSide;
private IListSide _listSide; /// <summary>
/// Constructor
/// </summary>
/// <param name="checkboxSide">Represent Checkbox/CheckboxX control</param>
/// <param name="listSide">Represent DataGridViewCheckBoxColumn</param>
public CheckboxAssociation(ICheckboxSide checkboxSide, IListSide listSide)
{
_checkboxSide = checkboxSide;
_checkboxSide.NotifyCheckedChanged = UpdateListSide; _listSide = listSide;
_listSide.NotifyCheckedChanged = UpdateCheckboxSide; UpdateCheckboxSide();
} /// <summary>
/// Update Checkbox by list
/// </summary>
public void UpdateCheckboxSide()
{
CheckState checkState;
if (_listSide.CheckedCount == _listSide.ItemsTotal && _listSide.CheckedCount != )
checkState = CheckState.Checked;
else if (_listSide.CheckedCount < _listSide.ItemsTotal && _listSide.CheckedCount != )
checkState = CheckState.Indeterminate;
else
checkState = CheckState.Unchecked;
_checkboxSide.UpdateCheckedProperty(checkState);
} /// <summary>
/// Update List item Checked Property value by Checkbox
/// </summary>
public void UpdateListSide()
{
_listSide.UpdateCheckedProperty(_checkboxSide.Checked);
} public void Dispose()
{
if (_checkboxSide != null)
{
_checkboxSide.Dispose();
_checkboxSide = null;
}
if (_listSide != null)
{
_listSide.Dispose();
_listSide = null;
}
}
}

5、CheckBox对应的ICheckboxSide

     /// <summary>
/// CheckBox
/// </summary>
public sealed class CheckBoxSide : ICheckboxSide
{
private CheckBox _checkBox; public CheckBoxSide(CheckBox checkBox)
{
_checkBox = checkBox;
_checkBox.MouseClick += _checkBox_MouseClick;
}
private void _checkBox_MouseClick(object sender, MouseEventArgs e)
{
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private bool IsEventValid()
{
var notOk = _checkBox == null || _checkBox.Disposing || _checkBox.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_checkBox != null)
{
_checkBox.MouseClick -= _checkBox_MouseClick;
_checkBox = null;
}
} /// <summary>
/// Get the Checked property value of Checkbox
/// </summary>
public bool Checked => _checkBox.Checked; /// <summary>
/// Notify others that my check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of Checkbox
/// </summary>
/// <param name="checkState"></param>
public void UpdateCheckedProperty(CheckState checkState)
{
_checkBox.CheckState = checkState;
}
}

6、Dotnetbar中的CheckBoxX对应的ICheckboxSide

     /// <summary>
/// CheckBox
/// </summary>
public sealed class CheckBoxXSide : ICheckboxSide
{
private CheckBoxX _checkBox; public CheckBoxXSide(CheckBoxX checkBox)
{
_checkBox = checkBox;
_checkBox.MouseClick += _checkBox_MouseClick;
}
private void _checkBox_MouseClick(object sender, MouseEventArgs e)
{
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private bool IsEventValid()
{
var notOk = _checkBox == null || _checkBox.Disposing || _checkBox.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_checkBox != null)
{
_checkBox.MouseClick -= _checkBox_MouseClick;
_checkBox = null;
}
} /// <summary>
/// Get the Checked property value of Checkbox
/// </summary>
public bool Checked => _checkBox.Checked; /// <summary>
/// Notify others that my check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of Checkbox
/// </summary>
/// <param name="checkState"></param>
public void UpdateCheckedProperty(CheckState checkState)
{
_checkBox.CheckState = checkState;
}
}

7、CheckedListBox对应的IListSide

     /// <summary>
/// DataGridViewCheckBoxColumn
/// </summary>
public class CheckedListBoxSide : IListSide
{
private CheckedListBox _checkedListBox; public CheckedListBoxSide(CheckedListBox checkedListBox)
{
_checkedListBox = checkedListBox;
_checkedListBox.CheckOnClick = true;
_checkedListBox.SelectedValueChanged += CheckedListBox_SelectedValueChanged;
CheckedListBox_SelectedValueChanged(checkedListBox, EventArgs.Empty);
} private void CheckedListBox_SelectedValueChanged(object sender, EventArgs e)
{
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private int GetCheckedCount()
{
return _checkedListBox.CheckedItems.Count;
} private bool IsEventValid()
{
var notOk = _checkedListBox == null || _checkedListBox.Disposing || _checkedListBox.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_checkedListBox != null)
{
_checkedListBox.SelectedValueChanged -= CheckedListBox_SelectedValueChanged;
_checkedListBox = null;
}
} /// <summary>
/// Get the total of all items
/// </summary>
public int ItemsTotal => _checkedListBox.Items.Count; /// <summary>
/// Get count of checked items
/// </summary>
public int CheckedCount => GetCheckedCount(); /// <summary>
/// Notify others that same items check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of all items
/// </summary>
/// <param name="setChecked"></param>
public void UpdateCheckedProperty(bool setChecked)
{
for (var i = ; i < _checkedListBox.Items.Count; i++)
{
_checkedListBox.SetItemChecked(i, setChecked);
}
}
}

8、DataGridViewCheckBoxColumn对应的IListSide

     /// <summary>
/// DataGridViewCheckBoxColumn
/// </summary>
public class DataGridViewCheckBoxColumnSide : IListSide
{
private DataGridViewCheckBoxColumn _checkBoxColumn; public DataGridViewCheckBoxColumnSide(DataGridViewCheckBoxColumn column)
{
_checkBoxColumn = column; _checkBoxColumn.DataGridView.CellContentClick += DataGridView_CellContentClick;
DataGridView_CellContentClick(_checkBoxColumn.DataGridView, new DataGridViewCellEventArgs(_checkBoxColumn.Index, ));
} private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != _checkBoxColumn.Index)
return;
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private int GetCheckedCount()
{
var checkedCount = ;
foreach (DataGridViewRow row in _checkBoxColumn.DataGridView.Rows)
{
var cell = row.Cells[_checkBoxColumn.Index] as DataGridViewCheckBoxCell;
if (cell?.EditedFormattedValue == null)
continue;
var cellBoolVal = false;
if (bool.TryParse(cell.EditedFormattedValue.ToString(), out cellBoolVal) && cellBoolVal)
++checkedCount;
}
return checkedCount;
} private bool IsEventValid()
{
var dgvw = _checkBoxColumn.DataGridView;
var notOk = dgvw == null || dgvw.Disposing || dgvw.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_checkBoxColumn?.DataGridView != null)
{
_checkBoxColumn.DataGridView.CellContentClick -= DataGridView_CellContentClick;
_checkBoxColumn = null;
}
} /// <summary>
/// Get the total of all items
/// </summary>
public int ItemsTotal => _checkBoxColumn.DataGridView.RowCount; /// <summary>
/// Get count of checked items
/// </summary>
public int CheckedCount => GetCheckedCount(); /// <summary>
/// Notify others that same items check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of all items
/// </summary>
/// <param name="setChecked"></param>
public void UpdateCheckedProperty(bool setChecked)
{
foreach (DataGridViewRow row in _checkBoxColumn.DataGridView.Rows)
{
var cell = row.Cells[_checkBoxColumn.Index] as DataGridViewCheckBoxCell;
if (cell?.EditedFormattedValue == null)
continue;
cell.Value = setChecked;
}
}
}

9、Dotnetbar中的ListBoxAdv控件对应的IListSide

     /// <summary>
/// ListBoxAdv
/// </summary>
public class ListBoxAdvSide : IListSide
{
private ListBoxAdv _listBoxAdv; public ListBoxAdvSide(ListBoxAdv listBoxAdv)
{
_listBoxAdv = listBoxAdv;
_listBoxAdv.ItemClick += ListBoxAdv_ItemClick;
ListBoxAdv_ItemClick(_listBoxAdv, EventArgs.Empty);
} private void ListBoxAdv_ItemClick(object sender, EventArgs e)
{
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private int GetCheckedCount()
{
return _listBoxAdv.CheckedItems.Count;
} private bool IsEventValid()
{
var notOk = _listBoxAdv == null || _listBoxAdv.Disposing || _listBoxAdv.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_listBoxAdv != null)
{
_listBoxAdv.ItemClick -= ListBoxAdv_ItemClick;
_listBoxAdv = null;
}
} /// <summary>
/// Get the total of all items
/// </summary>
public int ItemsTotal => _listBoxAdv.Items.Count; /// <summary>
/// Get count of checked items
/// </summary>
public int CheckedCount => GetCheckedCount(); /// <summary>
/// Notify others that same items check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of all items
/// </summary>
/// <param name="setChecked"></param>
public void UpdateCheckedProperty(bool setChecked)
{
var checkState = setChecked ? CheckState.Checked : CheckState.Unchecked;
for (var i = ; i < _listBoxAdv.Items.Count; i++)
{
_listBoxAdv.SetItemCheckState(i, checkState);
}
}
}

10、Dotnetbar中的DataGridViewCheckBoxX对应的IListSide

     /// <summary>
/// DataGridViewCheckBoxColumn
/// </summary>
public class DataGridViewCheckBoxXColumnSide : IListSide
{
private DataGridViewCheckBoxXColumn _checkBoxColumn; public DataGridViewCheckBoxXColumnSide(DataGridViewCheckBoxXColumn column)
{
_checkBoxColumn = column; _checkBoxColumn.DataGridView.CellContentClick += DataGridView_CellContentClick;
DataGridView_CellContentClick(_checkBoxColumn.DataGridView, new DataGridViewCellEventArgs(_checkBoxColumn.Index, ));
} private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != _checkBoxColumn.Index)
return;
if (!IsEventValid())
return;
NotifyCheckedChanged?.Invoke();
} private int GetCheckedCount()
{
var checkedCount = ;
foreach (DataGridViewRow row in _checkBoxColumn.DataGridView.Rows)
{
var cell = row.Cells[_checkBoxColumn.Index] as DataGridViewCheckBoxCell;
if (cell?.EditedFormattedValue == null)
continue;
var cellBoolVal = false;
if (bool.TryParse(cell.EditedFormattedValue.ToString(), out cellBoolVal) && cellBoolVal)
++checkedCount;
}
return checkedCount;
} private bool IsEventValid()
{
var dgvw = _checkBoxColumn.DataGridView;
var notOk = dgvw == null || dgvw.Disposing || dgvw.IsDisposed;
return !notOk;
} public void Dispose()
{
if (_checkBoxColumn?.DataGridView != null)
{
_checkBoxColumn.DataGridView.CellContentClick -= DataGridView_CellContentClick;
_checkBoxColumn = null;
}
} /// <summary>
/// Get the total of all items
/// </summary>
public int ItemsTotal => _checkBoxColumn.DataGridView.RowCount; /// <summary>
/// Get count of checked items
/// </summary>
public int CheckedCount => GetCheckedCount(); /// <summary>
/// Notify others that same items check property changed.
/// </summary>
public Action NotifyCheckedChanged { get; set; } /// <summary>
/// Set the check property value of all items
/// </summary>
/// <param name="setChecked"></param>
public void UpdateCheckedProperty(bool setChecked)
{
foreach (DataGridViewRow row in _checkBoxColumn.DataGridView.Rows)
{
var cell = row.Cells[_checkBoxColumn.Index] as DataGridViewCheckBoxCell;
if (cell?.EditedFormattedValue == null)
continue;
cell.Value = setChecked;
}
}
}

11、调用方法:窗体中存在chkPassAll(Checkbox)和chLBSystemTemp(CheckedListBox),

目的:使这两个控件联动,并且初始化为全选

             _chkAllSource = CheckboxAssociateFactroy.Create(chkPassAll, chLBSystemTemp);
chkPassAll.Checked = true;
_chkAllSource.UpdateListSide();

注意需要在窗体类内声明私有变量_chkAllSource

Winform中Checkbox与其他集合列表类型之间进行关联的更多相关文章

  1. DB2中字符、数字和日期类型之间的转换

    DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...

  2. Java中Date、String、Calendar类型之间的转化

    1.Calendar 转化 String   //获取当前时间的具体情况,如年,月,日,week,date,分,秒等   Calendar calendat = Calendar.getInstanc ...

  3. java中 列表,集合,数组之间的转换

    java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...

  4. Day2_数字类型_字符串类型_列表类型_元组_字典_集合_字符编码_文件处理

    数字类型: 作用:年纪,等级,薪资,身份证号等: 10进制转为2进制,利用bin来执行. 10进制转为8进制,利用oct来执行. 10进制转为16进制,利用hex来执行. #整型age=10 prin ...

  5. 聚合不应出现在 UPDATE 语句的集合列表中

    修改语句: update A set WZCount=ISNULL(WZCount,0)+(select SUM(WZCount) from T_PM_OutStock_SUB where Mater ...

  6. C# Winform中DataGridView的DataGridViewCheckBoxColumn CheckBox选中判断

    1.DataGridViewCheckBoxColumn CheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].F ...

  7. 总结day6 ---- set集合,基本类型的相互转化,编码,数据类型总结,循环时候不要动列表或者字典,深浅copy

    python小数据池,代码块的最详细.深入剖析   一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id ...

  8. 在Winform中直接录入表格数据和在Vue&Elment中直接录入表格数据的比较

    一般来说,录入数据的时候,我们都采用在一个窗体界面中,根据不同内容进行录入,但是有时候涉及主从表的数据录入,从表的数据有时候为了录入方便,也会通过表格控件直接录入.在Winform开发的时候,我们很多 ...

  9. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

随机推荐

  1. js避免命名冲突

    [1]工程师甲编写功能A var a = 1; var b = 2; alert(a+b); [2]工程师乙添加新功能B var a = 2; var b = 1; alert(a-b); [3]上一 ...

  2. Hive与表操作有关的语句

    Hive与表操作有关的语句 1.创建表的语句: Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COM ...

  3. 模拟一则ORA-600 [4194][][]故障并处理

    环境:OEL 5.7 + Oracle 11.2.0.3 1.模拟ORA-600 [4194][][]故障 2.使用bbed处理 3.尝试启动数据库 1.模拟ORA-600 [4194][][]故障 ...

  4. IT兄弟连 JavaWeb教程 jQuery中其他AJAX支持的函数

    ●  $.get()函数 $.get(url,data,function,dataType);参数说明如下: url:请求地址 data:请求参数 dataType:服务器返回的数据类型 functi ...

  5. 脑图和MarkDown

    使用脑图整理前端体系 根据以下网址整理 http://web.jobbole.com/84062/ http://naotu.baidu.com/ 学习MarkDown语法格式 (一) 标题 在首行插 ...

  6. Solidity 最新 0.5.8 中文文档发布

    本文首发于深入浅出区块链社区 热烈祝贺 Solidity 最新 0.5.8 中文文档发布, 这不单是一份 Solidity 速查手册,更是一份深入以太坊智能合约开发宝典. 翻译说明 Solidity ...

  7. 类variant解剖

    说明:由于代码较为庞大,类variant源码请参考\eos\libraries\fc\src中的variant.hpp与variant.cpp文件^_^.     首先概览一下这个庞大的类,细数一下, ...

  8. Linux上传下载工具 lrzsz

  9. CC08:翻转子串

    题目 假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串.请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次检查子串的函数. 给 ...

  10. Codeforces 1167E(思路、数据处理)

    思路 不难想到枚举\(l\),那如何高效求出最小的\(r\)?这样答案加上\(x-r+1\)即可. 如果\(l\)并没在序列里出现--没啥想法:如果\(l\)是序列里的数,我们可以做的事情是记下每个数 ...