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 ...
随机推荐
- try-catch-finally中return的执行情况
在try中没有异常的情况下try.catch.finally的执行顺序 try--- finally 如果try中有异常,执行顺序是try--- catch --- finally 如果try中没有异 ...
- 【245】◀▶IEW-Unit10
Unit 10 Censorship 1. Model1题目及范文分析 Some parents believe that there is no harm in allowing their chi ...
- Redux API之combineReducers
combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...
- Coding-Job:从研发到生产的容器化融合实践
大家好,我是来自 CODING 的全栈开发工程师,我有幸在 CODING 参与了 Coding-Job 这个容器化的编排平台的研发.大家对 CODING 可能比较了解, Coding.net 是一个一 ...
- <c++primer plus>学习笔记1之第八章函数探幽
1 c++内联函数 编译器将使用相应的函数代码替换函数调用,对于内联代码,函数无需跳到另一个位置执行代码再跳回来,所以内联函数运行速度比常规函数快. 但是代价是需要更多的内存. 使用场合: 执行函数代 ...
- HDU - 1869 六度分离 Floyd多源最短路
六度分离 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即 ...
- MVC实用笔记
---------------------------- 渲染一个Action:@{Html.RenderAction("Rulelist", "AjaxReuqestD ...
- 将Gridview导出到Excel
GridViewToExcel(EdceExcelGV, "application/ms-exce","xxxxxx表"); protected void Gr ...
- CentOS 安装配置vncserver
yum 安装tiger vncserver yum install tigervnc-server 安装后输入 vncserver 设置密码 3.配置用户 vim /etc/sysconfig/vnc ...
- cat命令详解及here doc
以下内容转自:http://www.cnblogs.com/peida/archive/2012/10/30/2746968.html cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文 ...