官网

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个按钮即可

添加属性

  /// <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自定义控件-穿梭框的更多相关文章

  1. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (八十)c#Winform自定义控件-分割线标签-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  3. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. “全栈2019”Java第八十二章:嵌套接口能否访问外部类中的成员?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  5. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (三十一)c#Winform自定义控件-文本框(四)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (五十)c#Winform自定义控件-滑块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. HDU 4719Oh My Holy FFF 线段树+DP

    /* ** 日期: 2013-9-12 ** 题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个.每个数有一个h[i], ** 每份最右边的那个数要大于前一份最右边的那个数.设每份最右边的 ...

  2. codeforces 389 D. Fox and Minimal path(构造+思维)

    题目链接:https://vjudge.net/contest/175446#problem/J 题解:显然要用最多n个点构成的图要使的得到的最短路条数有1e9次个,显然要有几个数相乘容易想到2的几进 ...

  3. 【Offer】[52] 【两个链表的第一个公共结点】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个链表,找出它们的第一个公共结点.下图中6为公共结点:  牛客网刷题地址 思路分析 如果两个链表有公共节点,那么公共节点出现在两 ...

  4. Fire Balls 10——UI界面的制作

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  5. js获取一个月的天数

    在使用其他语言获取每月天数的时候,一般都是存储到一个数组中进行获取,但是如果是二月份的话就需要首先判断是否闰年,再确定是28还是29了. js可以通过Date对象很方便的获取到每月的天数,在初始化Da ...

  6. 搭建自己的技术博客系列(二)把 Hexo 博客部署到 GitHub 上

    1.在GitHub上建一个新仓库

  7. Factory Method工厂方法模式

    定义一个用于创建对象的接口,让子类决定将哪一个类实例化.Factory Method使一个类的实例化延迟到其子类,属于创建型模式 在此模式中,工厂父类负责定义创建产品对象的公共接口,而工厂子类负责生产 ...

  8. 深入理解three.js中平面光光源RectAreaLight

    前言 之前有深入讲解过Three.js中光源,在那篇文章的最后也说了由于平面光光源的特殊性,所以会单独拿出来讲解,这篇文章会详细的讲解平面光光源的特性和实际应用该如何使用. 首先,平面光光源从一个矩形 ...

  9. Linux系统卡死后紧急处理

    前言:Linux系统卡死了的情况有很多,最常见的是系统负载过高导致的.还可以运行内存耗用极大的程序(如虚拟机),也会迅速提升系统负载.注意:不能再试图依赖任何图形界面的东西,如 Gnome的系统监视器 ...

  10. Winform中通过代码设置DevExpress的TextEdit的类型为Numbernic

    场景 使用DevExpress的EditText控件时,需要限制其输入类型为数字. 正常来说是窗体上拖拽一个TextEdit,然后在设计窗口点击小三角,选择Change Mask 但是如果说TextE ...