官網
http://www.hzhcontrols.com
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制元件,于是就有了本系列文章,
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
歡迎前來交流探討: 企鵝群568015492 
來都來了,點個【推薦】再走吧,謝謝
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[] LeftColumns18 {19 get { return leftColumns; }20 set21 {22 leftColumns = value;23 this.dgvLeft.Columns = leftColumns.ToList();24 }25 }26 27 /// <summary>28 /// The right columns29 /// </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[] RightColumns38 {39 get { return rightColumns; }40 set41 {42 rightColumns = value;43 this.dgvRight.Columns = leftColumns.ToList();44 }45 }46 47 /// <summary>48 /// The left data source49 /// </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[] LeftDataSource57 {58 get { return leftDataSource; }59 set60 {61 leftDataSource = value;62 dgvLeft.DataSource = value;63 }64 }65 66 /// <summary>67 /// The right data source68 /// </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[] RightDataSource76 {77 get { return rightDataSource; }78 set79 {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 /// or49 /// 左右資料源串列型別不一致,無法進行操作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:[email protected] 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 source102 /// </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[] RightDataSource110 {111 get { return rightDataSource; }112 set113 {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 /// or141 /// 左右資料源串列型別不一致,無法進行操作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 /// or181 /// 左右資料源串列型別不一致,無法進行操作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 點個星星吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/4910.html
標籤:WinForm
