(八十二)c#Winform自定义控件-穿梭框
程序员文章站
2023-11-25 16:58:22
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
这个用到了(一)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 }
最后的话
如果你喜欢的话,请到 点个星星吧
上一篇: 前端思考
下一篇: python学生管理系统