Winform中Checkbox与其他集合列表类型之间进行关联
本文提供了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与其他集合列表类型之间进行关联的更多相关文章
- DB2中字符、数字和日期类型之间的转换
DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...
- Java中Date、String、Calendar类型之间的转化
1.Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 Calendar calendat = Calendar.getInstanc ...
- java中 列表,集合,数组之间的转换
java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...
- Day2_数字类型_字符串类型_列表类型_元组_字典_集合_字符编码_文件处理
数字类型: 作用:年纪,等级,薪资,身份证号等: 10进制转为2进制,利用bin来执行. 10进制转为8进制,利用oct来执行. 10进制转为16进制,利用hex来执行. #整型age=10 prin ...
- 聚合不应出现在 UPDATE 语句的集合列表中
修改语句: update A set WZCount=ISNULL(WZCount,0)+(select SUM(WZCount) from T_PM_OutStock_SUB where Mater ...
- C# Winform中DataGridView的DataGridViewCheckBoxColumn CheckBox选中判断
1.DataGridViewCheckBoxColumn CheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].F ...
- 总结day6 ---- set集合,基本类型的相互转化,编码,数据类型总结,循环时候不要动列表或者字典,深浅copy
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id ...
- 在Winform中直接录入表格数据和在Vue&Elment中直接录入表格数据的比较
一般来说,录入数据的时候,我们都采用在一个窗体界面中,根据不同内容进行录入,但是有时候涉及主从表的数据录入,从表的数据有时候为了录入方便,也会通过表格控件直接录入.在Winform开发的时候,我们很多 ...
- winform中DataGridView实现分页功能
WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载) 转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...
随机推荐
- 在VS中对WCF服务进行更新,但是配置文件没有更新解决办法
取消下面的勾选框
- Hash表的实现
#include "stdafx.h" #include <iostream> #include <exception> using namespace s ...
- zkw线段树专题
题目来自大神博客的线段树专题 http://www.notonlysuccess.com/index.php/segment-tree-complete/ hdu1166 敌兵布阵题意:O(-1)思路 ...
- 2.6用tr进行转换
tr可以对来自标准输入的内容进行字符替换.字符删除以及重复字符压缩.它可以将一组字符变成另一组字符,因而通常也被称为转换命令. 1.tr只能通过stdin(标准输入),而无法通过命令行参数来接受输入. ...
- Umbraco back office 登录不了,如何解决
通过设置User的默认密码为"default", 它的Hash值为 bnWxWyFdCueCcKrqniYK9iAS+7E= 所以在SQL Server中执行以下脚本 UPDATE ...
- LIS与LCS的nlogn解法
LIS(nlogn) #include<iostream> #include<cstdio> using namespace std; ; int a[maxn]; int n ...
- HTML5学习笔记(五)存储
HTML5 web 存储,一个比cookie更好的本地存储方式.数据以 键/值 对存在, web网页的数据只允许该网页访问使用.加的安全与快速.可以存储大量的数据,而不影响网站的性能. 客户端存储数据 ...
- —Libre#2009. 「SCOI2015」小凸玩密室
#2009. 「SCOI2015」小凸玩密室 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- EOS帐户交易的构建命令
EOS版本:4.0 系统:Ubuntu 16.04 LTS 1.创建两对密匙 cleos create key Private key:5JeTwSwKfpVRHGLqysakTXfk ...
- Django模板语言,过滤器整理
Django模板语言,过滤器整理 1. add {{ value|add:"2" }} 把add后的参数加给value: 处理时,过滤器首先会强制把两个值转换成Int类型. 如果强 ...