博客
关于我
(八十二)c#Winform自定义控件-穿梭框-HZHControls
阅读量:415 次
发布时间:2019-03-06

本文共 13244 字,大约阅读时间需要 44 分钟。

官网

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:

码云:

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

用处及效果

准备工作

这个用到了、 、   不了解的可以先移步查看一下

开始

添加一个用户控件UCTestTransfer

界面放2个表格,2个按钮即可

添加属性

1  ///  2         /// 移动数据事件 3         ///  4         [Description("移动数据事件"), Category("自定义")] 5         public event TransferEventHandler Transfered; 6  7         ///  8         /// The left columns 9         /// 10         private DataGridViewColumnEntity[] leftColumns;11 12         /// 13         /// Gets or sets the left columns.14         /// 15         /// 
The left columns.
16 [Description("左侧列表列"), Category("自定义")]17 public DataGridViewColumnEntity[] LeftColumns18 {19 get { return leftColumns; }20 set21 {22 leftColumns = value;23 this.dgvLeft.Columns = leftColumns.ToList();24 }25 }26 27 /// 28 /// The right columns29 /// 30 private DataGridViewColumnEntity[] rightColumns;31 32 /// 33 /// Gets or sets the right columns.34 /// 35 ///
The right columns.
36 [Description("右侧列表列"), Category("自定义")]37 public DataGridViewColumnEntity[] RightColumns38 {39 get { return rightColumns; }40 set41 {42 rightColumns = value;43 this.dgvRight.Columns = leftColumns.ToList();44 }45 }46 47 /// 48 /// The left data source49 /// 50 private object[] leftDataSource;51 /// 52 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组53 /// 54 ///
The left data source.
55 [Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]56 public object[] LeftDataSource57 {58 get { return leftDataSource; }59 set60 {61 leftDataSource = value;62 dgvLeft.DataSource = value;63 }64 }65 66 /// 67 /// The right data source68 /// 69 private object[] rightDataSource;70 /// 71 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组72 /// 73 ///
The left data source.
74 [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]75 public object[] RightDataSource76 {77 get { return rightDataSource; }78 set79 {80 rightDataSource = value;81 dgvRight.DataSource = value;82 }83 }

处理左右移动按钮事件

1 ///  2         /// Handles the BtnClick event of the btnRight control. 3         ///  4         /// The source of the event. 5         /// The 
instance containing the event data. 6 ///
7 /// 左右数据源列表不能为空 8 /// or 9 /// 左右数据源列表类型不一致,无法进行操作10 ///
11 private void btnRight_BtnClick(object sender, EventArgs e)12 {13 if (LeftDataSource == null || RightDataSource == null)14 {15 throw new Exception("左右数据源列表不能为空");16 }17 if (LeftDataSource.GetType() != RightDataSource.GetType())18 {19 throw new Exception("左右数据源列表类型不一致,无法进行操作");20 }21 if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= 0)22 return;23 List lst = new List();24 dgvLeft.SelectRows.ForEach(p =>25 {26 lst.Add(p.DataSource);27 p.IsChecked = false;28 });29 var lstRight = RightDataSource.ToList();30 lstRight.AddRange(lst);31 var lstLeft = LeftDataSource.ToList();32 lstLeft.RemoveAll(p => lst.Contains(p));33 RightDataSource = lstRight.ToArray();34 LeftDataSource = lstLeft.ToArray();35 if (Transfered != null)36 {37 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });38 }39 }40 41 /// 42 /// Handles the BtnClick event of the btnLeft control.43 /// 44 /// The source of the event.45 /// The
instance containing the event data.46 ///
47 /// 左右数据源列表不能为空48 /// or49 /// 左右数据源列表类型不一致,无法进行操作50 ///
51 private void btnLeft_BtnClick(object sender, EventArgs e)52 {53 if (LeftDataSource == null || RightDataSource == null)54 {55 throw new Exception("左右数据源列表不能为空");56 }57 if (LeftDataSource.GetType() != RightDataSource.GetType())58 {59 throw new Exception("左右数据源列表类型不一致,无法进行操作");60 }61 if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= 0)62 return;63 List lst = new List();64 dgvRight.SelectRows.ForEach(p =>65 {66 lst.Add(p.DataSource);67 p.IsChecked = false;68 });69 var lstLeft = LeftDataSource.ToList();70 lstLeft.AddRange(lst);71 var lstRight = RightDataSource.ToList();72 lstRight.RemoveAll(p => lst.Contains(p));73 RightDataSource = lstRight.ToArray();74 LeftDataSource = lstLeft.ToArray();75 if (Transfered != null)76 {77 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });78 }79 }

完整代码

1 // ***********************************************************************  2 // Assembly         : HZH_Controls  3 // Created          : 2019-10-10  4 //  5 // ***********************************************************************  6 // 
7 // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com 8 //
9 // 10 // Blog: https://www.cnblogs.com/bfyx 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 13 // 14 // If you use this code, please keep this note. 15 // *********************************************************************** 16 using System; 17 using System.Collections.Generic; 18 using System.ComponentModel; 19 using System.Drawing; 20 using System.Data; 21 using System.Linq; 22 using System.Text; 23 using System.Windows.Forms; 24 25 namespace HZH_Controls.Controls 26 { 27 /// 28 /// Class UCTransfer. 29 /// Implements the
30 ///
31 ///
32 [DefaultEvent("Transfered")] 33 public partial class UCTransfer : UserControl 34 { 35 /// 36 /// 移动数据事件 37 /// 38 [Description("移动数据事件"), Category("自定义")] 39 public event TransferEventHandler Transfered; 40 41 /// 42 /// The left columns 43 /// 44 private DataGridViewColumnEntity[] leftColumns; 45 46 /// 47 /// Gets or sets the left columns. 48 /// 49 ///
The left columns.
50 [Description("左侧列表列"), Category("自定义")] 51 public DataGridViewColumnEntity[] LeftColumns 52 { 53 get { return leftColumns; } 54 set 55 { 56 leftColumns = value; 57 this.dgvLeft.Columns = leftColumns.ToList(); 58 } 59 } 60 61 /// 62 /// The right columns 63 /// 64 private DataGridViewColumnEntity[] rightColumns; 65 66 /// 67 /// Gets or sets the right columns. 68 /// 69 ///
The right columns.
70 [Description("右侧列表列"), Category("自定义")] 71 public DataGridViewColumnEntity[] RightColumns 72 { 73 get { return rightColumns; } 74 set 75 { 76 rightColumns = value; 77 this.dgvRight.Columns = leftColumns.ToList(); 78 } 79 } 80 81 /// 82 /// The left data source 83 /// 84 private object[] leftDataSource; 85 /// 86 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 87 /// 88 ///
The left data source.
89 [Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 90 public object[] LeftDataSource 91 { 92 get { return leftDataSource; } 93 set 94 { 95 leftDataSource = value; 96 dgvLeft.DataSource = value; 97 } 98 } 99 100 /// 101 /// The right data source102 /// 103 private object[] rightDataSource;104 /// 105 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组106 /// 107 ///
The left data source.
108 [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]109 public object[] RightDataSource110 {111 get { return rightDataSource; }112 set113 {114 rightDataSource = value;115 dgvRight.DataSource = value;116 }117 }118 119 /// 120 /// Initializes a new instance of the
class.121 ///
122 public UCTransfer()123 {124 InitializeComponent();125 dgvLeft.IsCloseAutoHeight = true;126 dgvRight.IsCloseAutoHeight = true;127 LeftColumns = new DataGridViewColumnEntity[0];128 RightColumns = new DataGridViewColumnEntity[0];129 LeftDataSource = new object[0];130 RightDataSource = new object[0];131 }132 133 /// 134 /// Handles the BtnClick event of the btnRight control.135 /// 136 /// The source of the event.137 /// The
instance containing the event data.138 ///
139 /// 左右数据源列表不能为空140 /// or141 /// 左右数据源列表类型不一致,无法进行操作142 ///
143 private void btnRight_BtnClick(object sender, EventArgs e)144 {145 if (LeftDataSource == null || RightDataSource == null)146 {147 throw new Exception("左右数据源列表不能为空");148 }149 if (LeftDataSource.GetType() != RightDataSource.GetType())150 {151 throw new Exception("左右数据源列表类型不一致,无法进行操作");152 }153 if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= 0)154 return;155 List lst = new List();156 dgvLeft.SelectRows.ForEach(p =>157 {158 lst.Add(p.DataSource);159 p.IsChecked = false;160 });161 var lstRight = RightDataSource.ToList();162 lstRight.AddRange(lst);163 var lstLeft = LeftDataSource.ToList();164 lstLeft.RemoveAll(p => lst.Contains(p));165 RightDataSource = lstRight.ToArray();166 LeftDataSource = lstLeft.ToArray();167 if (Transfered != null)168 {169 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });170 }171 }172 173 /// 174 /// Handles the BtnClick event of the btnLeft control.175 /// 176 /// The source of the event.177 /// The
instance containing the event data.178 ///
179 /// 左右数据源列表不能为空180 /// or181 /// 左右数据源列表类型不一致,无法进行操作182 ///
183 private void btnLeft_BtnClick(object sender, EventArgs e)184 {185 if (LeftDataSource == null || RightDataSource == null)186 {187 throw new Exception("左右数据源列表不能为空");188 }189 if (LeftDataSource.GetType() != RightDataSource.GetType())190 {191 throw new Exception("左右数据源列表类型不一致,无法进行操作");192 }193 if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= 0)194 return;195 List lst = new List();196 dgvRight.SelectRows.ForEach(p =>197 {198 lst.Add(p.DataSource);199 p.IsChecked = false;200 });201 var lstLeft = LeftDataSource.ToList();202 lstLeft.AddRange(lst);203 var lstRight = RightDataSource.ToList();204 lstRight.RemoveAll(p => lst.Contains(p));205 RightDataSource = lstRight.ToArray();206 LeftDataSource = lstLeft.ToArray();207 if (Transfered != null)208 {209 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });210 }211 }212 }213 }

 

最后的话

如果你喜欢的话,请到  点个星星吧

你可能感兴趣的文章
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>