(八十二)c#Winform自定义控件-穿梭框
官网
前提
入行已经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个按钮即可
添加属性
/// <summary>
/// 移动数据事件
/// </summary>
[Description("移动数据事件"), Category("自定义")]
public event TransferEventHandler Transfered; /// <summary>
/// The left columns
/// </summary>
private DataGridViewColumnEntity[] leftColumns; /// <summary>
/// Gets or sets the left columns.
/// </summary>
/// <value>The left columns.</value>
[Description("左侧列表列"), Category("自定义")]
public DataGridViewColumnEntity[] LeftColumns
{
get { return leftColumns; }
set
{
leftColumns = value;
this.dgvLeft.Columns = leftColumns.ToList();
}
} /// <summary>
/// The right columns
/// </summary>
private DataGridViewColumnEntity[] rightColumns; /// <summary>
/// Gets or sets the right columns.
/// </summary>
/// <value>The right columns.</value>
[Description("右侧列表列"), Category("自定义")]
public DataGridViewColumnEntity[] RightColumns
{
get { return rightColumns; }
set
{
rightColumns = value;
this.dgvRight.Columns = leftColumns.ToList();
}
} /// <summary>
/// The left data source
/// </summary>
private object[] leftDataSource;
/// <summary>
/// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
/// </summary>
/// <value>The left data source.</value>
[Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public object[] LeftDataSource
{
get { return leftDataSource; }
set
{
leftDataSource = value;
dgvLeft.DataSource = value;
}
} /// <summary>
/// The right data source
/// </summary>
private object[] rightDataSource;
/// <summary>
/// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
/// </summary>
/// <value>The left data source.</value>
[Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public object[] RightDataSource
{
get { return rightDataSource; }
set
{
rightDataSource = value;
dgvRight.DataSource = value;
}
}
处理左右移动按钮事件
/// <summary>
/// Handles the BtnClick event of the btnRight control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <exception cref="System.Exception">
/// 左右数据源列表不能为空
/// or
/// 左右数据源列表类型不一致,无法进行操作
/// </exception>
private void btnRight_BtnClick(object sender, EventArgs e)
{
if (LeftDataSource == null || RightDataSource == null)
{
throw new Exception("左右数据源列表不能为空");
}
if (LeftDataSource.GetType() != RightDataSource.GetType())
{
throw new Exception("左右数据源列表类型不一致,无法进行操作");
}
if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= )
return;
List<object> lst = new List<object>();
dgvLeft.SelectRows.ForEach(p =>
{
lst.Add(p.DataSource);
p.IsChecked = false;
});
var lstRight = RightDataSource.ToList();
lstRight.AddRange(lst);
var lstLeft = LeftDataSource.ToList();
lstLeft.RemoveAll(p => lst.Contains(p));
RightDataSource = lstRight.ToArray();
LeftDataSource = lstLeft.ToArray();
if (Transfered != null)
{
Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });
}
} /// <summary>
/// Handles the BtnClick event of the btnLeft control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <exception cref="System.Exception">
/// 左右数据源列表不能为空
/// or
/// 左右数据源列表类型不一致,无法进行操作
/// </exception>
private void btnLeft_BtnClick(object sender, EventArgs e)
{
if (LeftDataSource == null || RightDataSource == null)
{
throw new Exception("左右数据源列表不能为空");
}
if (LeftDataSource.GetType() != RightDataSource.GetType())
{
throw new Exception("左右数据源列表类型不一致,无法进行操作");
}
if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= )
return;
List<object> lst = new List<object>();
dgvRight.SelectRows.ForEach(p =>
{
lst.Add(p.DataSource);
p.IsChecked = false;
});
var lstLeft = LeftDataSource.ToList();
lstLeft.AddRange(lst);
var lstRight = RightDataSource.ToList();
lstRight.RemoveAll(p => lst.Contains(p));
RightDataSource = lstRight.ToArray();
LeftDataSource = lstLeft.ToArray();
if (Transfered != null)
{
Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });
}
}
完整代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-10-10
//
// ***********************************************************************
// <copyright file="UCTransfer.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
/// <summary>
/// Class UCTransfer.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
[DefaultEvent("Transfered")]
public partial class UCTransfer : UserControl
{
/// <summary>
/// 移动数据事件
/// </summary>
[Description("移动数据事件"), Category("自定义")]
public event TransferEventHandler Transfered; /// <summary>
/// The left columns
/// </summary>
private DataGridViewColumnEntity[] leftColumns; /// <summary>
/// Gets or sets the left columns.
/// </summary>
/// <value>The left columns.</value>
[Description("左侧列表列"), Category("自定义")]
public DataGridViewColumnEntity[] LeftColumns
{
get { return leftColumns; }
set
{
leftColumns = value;
this.dgvLeft.Columns = leftColumns.ToList();
}
} /// <summary>
/// The right columns
/// </summary>
private DataGridViewColumnEntity[] rightColumns; /// <summary>
/// Gets or sets the right columns.
/// </summary>
/// <value>The right columns.</value>
[Description("右侧列表列"), Category("自定义")]
public DataGridViewColumnEntity[] RightColumns
{
get { return rightColumns; }
set
{
rightColumns = value;
this.dgvRight.Columns = leftColumns.ToList();
}
} /// <summary>
/// The left data source
/// </summary>
private object[] leftDataSource;
/// <summary>
/// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
/// </summary>
/// <value>The left data source.</value>
[Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public object[] LeftDataSource
{
get { return leftDataSource; }
set
{
leftDataSource = value;
dgvLeft.DataSource = value;
}
} /// <summary>
/// The right data source
/// </summary>
private object[] rightDataSource;
/// <summary>
/// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
/// </summary>
/// <value>The left data source.</value>
[Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public object[] RightDataSource
{
get { return rightDataSource; }
set
{
rightDataSource = value;
dgvRight.DataSource = value;
}
} /// <summary>
/// Initializes a new instance of the <see cref="UCTransfer"/> class.
/// </summary>
public UCTransfer()
{
InitializeComponent();
dgvLeft.IsCloseAutoHeight = true;
dgvRight.IsCloseAutoHeight = true;
LeftColumns = new DataGridViewColumnEntity[];
RightColumns = new DataGridViewColumnEntity[];
LeftDataSource = new object[];
RightDataSource = new object[];
} /// <summary>
/// Handles the BtnClick event of the btnRight control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <exception cref="System.Exception">
/// 左右数据源列表不能为空
/// or
/// 左右数据源列表类型不一致,无法进行操作
/// </exception>
private void btnRight_BtnClick(object sender, EventArgs e)
{
if (LeftDataSource == null || RightDataSource == null)
{
throw new Exception("左右数据源列表不能为空");
}
if (LeftDataSource.GetType() != RightDataSource.GetType())
{
throw new Exception("左右数据源列表类型不一致,无法进行操作");
}
if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= )
return;
List<object> lst = new List<object>();
dgvLeft.SelectRows.ForEach(p =>
{
lst.Add(p.DataSource);
p.IsChecked = false;
});
var lstRight = RightDataSource.ToList();
lstRight.AddRange(lst);
var lstLeft = LeftDataSource.ToList();
lstLeft.RemoveAll(p => lst.Contains(p));
RightDataSource = lstRight.ToArray();
LeftDataSource = lstLeft.ToArray();
if (Transfered != null)
{
Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });
}
} /// <summary>
/// Handles the BtnClick event of the btnLeft control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <exception cref="System.Exception">
/// 左右数据源列表不能为空
/// or
/// 左右数据源列表类型不一致,无法进行操作
/// </exception>
private void btnLeft_BtnClick(object sender, EventArgs e)
{
if (LeftDataSource == null || RightDataSource == null)
{
throw new Exception("左右数据源列表不能为空");
}
if (LeftDataSource.GetType() != RightDataSource.GetType())
{
throw new Exception("左右数据源列表类型不一致,无法进行操作");
}
if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= )
return;
List<object> lst = new List<object>();
dgvRight.SelectRows.ForEach(p =>
{
lst.Add(p.DataSource);
p.IsChecked = false;
});
var lstLeft = LeftDataSource.ToList();
lstLeft.AddRange(lst);
var lstRight = RightDataSource.ToList();
lstRight.RemoveAll(p => lst.Contains(p));
RightDataSource = lstRight.ToArray();
LeftDataSource = lstLeft.ToArray();
if (Transfered != null)
{
Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });
}
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(八十二)c#Winform自定义控件-穿梭框的更多相关文章
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (八十)c#Winform自定义控件-分割线标签-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- “全栈2019”Java第八十二章:嵌套接口能否访问外部类中的成员?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十一)c#Winform自定义控件-文本框(四)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十)c#Winform自定义控件-滑块
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (二十八)c#Winform自定义控件-文本框(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- CodeForces 85D Sum of Medians Splay | 线段树
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...
- CF - 1108 E 枚举上界+线段树维护
题目传送门 枚举每个点作为最大值的那个点.然后既然是作为最大值出现的话,那么这个点就是不需要被减去的,因为如果最小值也在这个区间内的话,2者都减去1,对答案没有影响,如果是最小值不出现在这个区间内的话 ...
- 一起来聊一下 JavaScript 的用途和那些特性
JavaScript 简介 我们一起来聊一下 JavaScript,用它能做什么,它有哪些特性,以及一些跟它配合使用的技术. 什么是 JavaScript? JavaScript 最初的目的是为了&q ...
- 简单粗暴详细讲解javascript实现函数柯里化
函数柯里化(黑人问号脸)???Currying(黑人问号脸)???妥妥的中式翻译既视感:下面来一起看看究竟什么是函数柯里化: 维基百科的解释是:把接收多个参数的函数变换成接收一个单一参数(最初函数的第 ...
- Vue 前端uni-app多环境配置部署服务器的问题
目录 前端Vue 针对问题 package.json描述 多环境部署 查看源码获取解决方案 转载请标明出处: http://dujinyang.blog.csdn.net/ 本文出自:[奥特曼超人的博 ...
- html5表单与Jquery Ajax配合使用
html5的表单控件提供了很多格式检测功能,可以省去很多烦人的javascript验证代码,例如pattern属性和require属性,但触发的条件是表单提交,如果想通过ajax提交表单,就出现了不能 ...
- HBase的表结构
HBase以表的形式存储数据.表有行和列组成.列划分为若干个列族/列簇(column family). 如上图所示,key1,key2,key3是三条记录的唯一的row key值,column-fa ...
- Linux之vim、压缩与解压缩
终有一种方法适合你!
- 使用Consul做leader选举的方案
在分布式集群部署模式下,为了维护数据一致性,通常需要选举出一个leader来进行协调,并且在leader挂掉后能从集群中选举出一个新的leader.选举leader的方案有很多种,对Paxos和Raf ...
- 【linux】【Go】Centos7安装go1.13环境
前言 Go(又称Golang)是Google开发的一种静态强类型.编译型.并发型,并具有垃圾回收功能的编程语言. 罗伯特·格瑞史莫(Robert Griesemer),罗勃·派克(Rob Pi ...