本文共 13244 字,大约阅读时间需要 44 分钟。
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
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 /// Theinstance 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
完整代码
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 31 ///30 /// 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 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 ///class.121 /// 134 /// Handles the BtnClick event of the btnRight control.135 /// 136 /// The source of the event.137 /// Theinstance 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 Listlst = 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 /// Theinstance 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 Listlst = 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 }
如果你喜欢的话,请到 点个星星吧