前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
这个用到了(一)c#Winform自定义控件-基类控件、(三)c#Winform自定义控件-有图标的按钮 、 (三十二)c#Winform自定义控件-表格 不了解的可以先移步查看一下
开始
添加一个用户控件UCTestTransfer
界面放2个表格,2个按钮即可
添加属性
1 /// <summary> 2 /// 移动数据事件 3 /// </summary> 4 [Description("移动数据事件"), Category("自定义")] 5 public event TransferEventHandler Transfered; 6 7 /// <summary> 8 /// The left columns 9 /// </summary> 10 private DataGridViewColumnEntity[] leftColumns; 11 12 /// <summary> 13 /// Gets or sets the left columns. 14 /// </summary> 15 /// <value>The left columns.</value> 16 [Description("左侧列表列"), Category("自定义")] 17 public DataGridViewColumnEntity[] LeftColumns 18 { 19 get { return leftColumns; } 20 set 21 { 22 leftColumns = value; 23 this.dgvLeft.Columns = leftColumns.ToList(); 24 } 25 } 26 27 /// <summary> 28 /// The right columns 29 /// </summary> 30 private DataGridViewColumnEntity[] rightColumns; 31 32 /// <summary> 33 /// Gets or sets the right columns. 34 /// </summary> 35 /// <value>The right columns.</value> 36 [Description("右侧列表列"), Category("自定义")] 37 public DataGridViewColumnEntity[] RightColumns 38 { 39 get { return rightColumns; } 40 set 41 { 42 rightColumns = value; 43 this.dgvRight.Columns = leftColumns.ToList(); 44 } 45 } 46 47 /// <summary> 48 /// The left data source 49 /// </summary> 50 private object[] leftDataSource; 51 /// <summary> 52 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 53 /// </summary> 54 /// <value>The left data source.</value> 55 [Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 56 public object[] LeftDataSource 57 { 58 get { return leftDataSource; } 59 set 60 { 61 leftDataSource = value; 62 dgvLeft.DataSource = value; 63 } 64 } 65 66 /// <summary> 67 /// The right data source 68 /// </summary> 69 private object[] rightDataSource; 70 /// <summary> 71 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 72 /// </summary> 73 /// <value>The left data source.</value> 74 [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 75 public object[] RightDataSource 76 { 77 get { return rightDataSource; } 78 set 79 { 80 rightDataSource = value; 81 dgvRight.DataSource = value; 82 } 83 }
处理左右移动按钮事件
1 /// <summary> 2 /// Handles the BtnClick event of the btnRight control. 3 /// </summary> 4 /// <param name="sender">The source of the event.</param> 5 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 6 /// <exception cref="System.Exception"> 7 /// 左右数据源列表不能为空 8 /// or 9 /// 左右数据源列表类型不一致,无法进行操作 10 /// </exception> 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<object> lst = new List<object>(); 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 /// <summary> 42 /// Handles the BtnClick event of the btnLeft control. 43 /// </summary> 44 /// <param name="sender">The source of the event.</param> 45 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 46 /// <exception cref="System.Exception"> 47 /// 左右数据源列表不能为空 48 /// or 49 /// 左右数据源列表类型不一致,无法进行操作 50 /// </exception> 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<object> lst = new List<object>(); 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 // <copyright file="UCTransfer.cs"> 7 // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com 8 // </copyright> 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 /// <summary> 28 /// Class UCTransfer. 29 /// Implements the <see cref="System.Windows.Forms.UserControl" /> 30 /// </summary> 31 /// <seealso cref="System.Windows.Forms.UserControl" /> 32 [DefaultEvent("Transfered")] 33 public partial class UCTransfer : UserControl 34 { 35 /// <summary> 36 /// 移动数据事件 37 /// </summary> 38 [Description("移动数据事件"), Category("自定义")] 39 public event TransferEventHandler Transfered; 40 41 /// <summary> 42 /// The left columns 43 /// </summary> 44 private DataGridViewColumnEntity[] leftColumns; 45 46 /// <summary> 47 /// Gets or sets the left columns. 48 /// </summary> 49 /// <value>The left columns.</value> 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 /// <summary> 62 /// The right columns 63 /// </summary> 64 private DataGridViewColumnEntity[] rightColumns; 65 66 /// <summary> 67 /// Gets or sets the right columns. 68 /// </summary> 69 /// <value>The right columns.</value> 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 /// <summary> 82 /// The left data source 83 /// </summary> 84 private object[] leftDataSource; 85 /// <summary> 86 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 87 /// </summary> 88 /// <value>The left data source.</value> 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 /// <summary> 101 /// The right data source 102 /// </summary> 103 private object[] rightDataSource; 104 /// <summary> 105 /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 106 /// </summary> 107 /// <value>The left data source.</value> 108 [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 109 public object[] RightDataSource 110 { 111 get { return rightDataSource; } 112 set 113 { 114 rightDataSource = value; 115 dgvRight.DataSource = value; 116 } 117 } 118 119 /// <summary> 120 /// Initializes a new instance of the <see cref="UCTransfer"/> class. 121 /// </summary> 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 /// <summary> 134 /// Handles the BtnClick event of the btnRight control. 135 /// </summary> 136 /// <param name="sender">The source of the event.</param> 137 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 138 /// <exception cref="System.Exception"> 139 /// 左右数据源列表不能为空 140 /// or 141 /// 左右数据源列表类型不一致,无法进行操作 142 /// </exception> 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<object> lst = new List<object>(); 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 /// <summary> 174 /// Handles the BtnClick event of the btnLeft control. 175 /// </summary> 176 /// <param name="sender">The source of the event.</param> 177 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 178 /// <exception cref="System.Exception"> 179 /// 左右数据源列表不能为空 180 /// or 181 /// 左右数据源列表类型不一致,无法进行操作 182 /// </exception> 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<object> lst = new List<object>(); 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 }
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧