前提
入行已经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 /// 左右数据