(八十二)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 ...
随机推荐
- E-MAZE_2019牛客暑期多校训练营(第二场)
题意 给出n行m列的迷宫0可走1不可走,有两个操作,操作1变换点(a,b)的值,操作2查询(1,a)到(n,b)的方案数 题解 设\(F[i][j]\)为第i-1行到达第i行第j列的方案数,若点\(( ...
- hdu 6215 Brute Force Sorting(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...
- UOJ 34 多项式乘法 FFT 模板
这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项 ...
- 剑指offer(五):用两个栈实现一个队列
题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解决办法: 队列先进先出,栈先进后出(stack1和stack2) 其实主要要注意的点是: ①在添加时直接 ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)-服务端(配置中心)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- EasyUI总结(一)-- 入门
一.EasyUI下载 EasyUI官方下载地址:http://www.jeasyui.com/download/index.php,目前最新的版本是:jQuery EasyUI 1.4.1
- 初步认识JWT
前言: 现在越来越多的项目或多或少会用到JWT,为什么会出现使用JWT这样的场景的呢? 假设现在有一个APP,后台是分布式系统.APP的首页模块部署在上海机房的服务器上,子页面模块部署在深圳机房的服务 ...
- 实验吧CTF练习题---web---登录一下好吗解析
实验吧web之登陆一下好么 地址:http://www.shiyanbar.com/ctf/1942 flag值:ctf{51d1bf8fb65a8c2406513ee8f52283e7} 解 ...
- SpringBoot 2.0 + 阿里巴巴 Sentinel 动态限流实战
前言 在从0到1构建分布式秒杀系统和打造十万博文系统中,限流是不可缺少的一个环节,在系统能承受的范围内既能减少资源开销又能防御恶意攻击. 在前面的文章中,我们使用了开源工具包 Guava 提供的限流工 ...
- 【第十一篇】这一篇来说说MVC+EF+easyui datagrid的查询功能
老规矩 直接上代码 <form class="form-horizontal"> <div class="box-body"> < ...