(八十二)c#Winform自定义控件-穿梭框

前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

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

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

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

(八十二)c#Winform自定义控件-穿梭框_第1张图片

准备工作

这个用到了(一)c#Winform自定义控件-基类控件、(三)c#Winform自定义控件-有图标的按钮 、 (三十二)c#Winform自定义控件-表格  不了解的可以先移步查看一下

开始

添加一个用户控件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[] LeftColumns
18         {
19             get { return leftColumns; }
20             set
21             {
22                 leftColumns = value;
23                 this.dgvLeft.Columns = leftColumns.ToList();
24             }
25         }
26 
27         /// 
28         /// The right columns
29         /// 
30         private DataGridViewColumnEntity[] rightColumns;
31 
32         /// 
33         /// Gets or sets the right columns.
34         /// 
35         /// The right columns.
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         /// 
48         /// The left data source
49         /// 
50         private object[] leftDataSource;
51         /// 
52         /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
53         /// 
54         /// The left data source.
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         /// 
67         /// The right data source
68         /// 
69         private object[] rightDataSource;
70         /// 
71         /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
72         /// 
73         /// The left data source.
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 /// 
 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<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         /// 
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         /// or
49         /// 左右数据源列表类型不一致,无法进行操作
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<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 // 
  7 //     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:[email protected]
  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 source
102         /// 
103         private object[] rightDataSource;
104         /// 
105         /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
106         /// 
107         /// The left data source.
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         /// 
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         /// or
141         /// 左右数据源列表类型不一致,无法进行操作
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<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         /// 
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         /// or
181         /// 左右数据源列表类型不一致,无法进行操作
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<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 点个星星吧

你可能感兴趣的:((八十二)c#Winform自定义控件-穿梭框)