本文提供了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

最新文章

  1. 构造函数忘记new? 看这里看这里
  2. 标准差(standard deviation)和标准误差(standard error)你能解释清楚吗?
  3. (转)Tomcat 7 访问 Manager 和 Host Manager
  4. python基础教程_学习笔记10:异常
  5. 新RSS reader
  6. Windows下搭建PHP开发环境【总结】
  7. 头文件limits—各个类型的数据的范围
  8. FastDFS分布文件系统相关资料索引
  9. Pycharm配置(二)
  10. PHPCMS v9.6.0 wap模块 SQL注入
  11. windows 安装zookeeper
  12. mysql查看编码格式以及修改编码格式
  13. 【Impala篇】---Hue从初始到安装应用
  14. JAVA发红包案例
  15. Python函数--装饰器进阶
  16. TCP/IP 笔记 - 超时和重传
  17. 深入 kernel panic 流程【转】
  18. Apache tica详述
  19. linux命令学习之:sort
  20. 【转】对 Rust 语言的分析

热门文章

  1. deleteMany is not a function
  2. 11_listview入门
  3. Struts&amp;nbsp;result&amp;nbsp;param详细设置
  4. posix 正则库程序
  5. Ubuntu中的minicom
  6. 模板 - 动态规划 - 数位dp
  7. solidity学习笔记
  8. Python学习笔记(随机数)
  9. ios 实现 cell 的动态高度
  10. Mac环境下制作ubantu安装盘