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 ...
随机推荐
- NLB
http://www.cnblogs.com/allegro/archive/2011/02/11/1951171.html
- 《精通Spring4.X企业应用开发实战》读后感第四章(资源访问)
package com.smart.resource; import org.springframework.core.io.ClassPathResource; import org.springf ...
- 3-C++程序的结构1.5
多文件结构和编译预处理命令 1.c++程序的一般组织结构 通常一个项目至少划分为三个文件:类定义文件(*.h文件).类实现文件(*.cpp文件)和类的使用文件(*.cpp,主函数文件).如下: 这三个 ...
- java之数学方法
参考http://how2j.cn/k/number-string/number-string-math/319.html java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法 ...
- layer常用方法
弹出层layer的使用 弹出层layer的使用 Intro layer是一款web弹层组件,致力于服务各个水平段的开发人员.layer官网:http://layer.layui.com/ layer侧 ...
- 滴滴Booster移动APP质量优化框架 学习之旅
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) 一.Booster简介 Booster是滴滴最近开源一个的移动应 ...
- “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)
一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增删改查的地步.测试好之后再来看这篇文章,我的主框架相对来说简答一点,重点还是实现系统的功能,以及对Jquery-Eas ...
- 用matplotlib画线
1:matplotlib基础 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形[1] . 通过 Matplotlib,开发者可以 ...
- C++ STL vector使用总结
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说vect ...
- uva11357 Matches
Matches UVA - 11375 题意: 给你n根matches, 你可以拼出多少个数字0~9. 不必全部用完. 解题思路: 1. 计数题, 本题可以用图来理解. 把"已经使用了i根m ...