入门级: WinForm 下的 ComboBox,ListBox 的使用 (三) 选择控件

平常我们经常会用到这样的控件:选中一个ListBox 中的项移动到另外一个 ListBox 中,反过来也行。像下面这样:

 

我们需要将上面两个涂黑的地方 (Label),以及两个 ListBox,四个操作按钮做成为一个控件,这样方便重复利用,也会少写很多代码

在项目上点击“添加用户控件”,命名为: ListBoxLR

然后在控件上添加上面共8个控件,然后需要为该控件添加至少4个事件:

 

 

复制代码
        [Category("Action")]
        
public event EventHandler AllToLeftClick = null;// 全选到左边
        [Category("Action")]
        
public event EventHandler AllToRightClick = null;// 全选到右边
        [Category("Action")]
        
public event EventHandler OneToLeftClick = null;// 选一个到左边
        [Category("Action")]
        
public event EventHandler OneToRightClick = null;// 选一个到右边
复制代码

 

 

 

两个涂黑的地方为左右标题,也提供自定义:

 

复制代码
        [Category("Appearance")]
        
public string TextLeft {
            
get {
                
return lblLeft.Text;
            }
            
set {
                lblLeft.Text 
= value;
            }
        }

        [Category(
"Appearance")]
        
public string TextRight {
            
get {
                
return lblRight.Text;
            }
            
set {
                lblRight.Text 
= value;
            }
        }

复制代码

 

 

接着是两个 ListBox:

 

复制代码

        [Browsable(
false)]
        
public ListBox BoxLeft {
            
get {
                
return lstLeft;
            }
        }

        [Browsable(
false)]
        
public ListBox BoxRight {
            
get {
                
return lstRight;
            }
        }
复制代码

 

 

以下是完整源码:

 

 

复制代码
完整源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Lyout.Controls {
    
/// <summary>
    
/// 功能:左右交换列表控件
    
/// 日期:2010-09-20     修改日期:
    
/// 作者:夏荣全         修改人:
    
/// 联系:lyout@163.com QQ:249775085
    
/// </summary>
    public partial class ListBoxLR : UserControl {
        [Category(
"Action")]
        
public event EventHandler AllToLeftClick = null;
        [Category(
"Action")]
        
public event EventHandler AllToRightClick = null;
        [Category(
"Action")]
        
public event EventHandler OneToLeftClick = null;
        [Category(
"Action")]
        
public event EventHandler OneToRightClick = null;

        
public ListBoxLR() {
            InitializeComponent();
        }

        [Category(
"Appearance")]
        
public string TextLeft {
            
get {
                
return lblLeft.Text;
            }
            
set {
                lblLeft.Text 
= value;
            }
        }

        [Category(
"Appearance")]
        
public string TextRight {
            
get {
                
return lblRight.Text;
            }
            
set {
                lblRight.Text 
= value;
            }
        }

        [Browsable(
false)]
        
public ListBox BoxLeft {
            
get {
                
return lstLeft;
            }
        }

        [Browsable(
false)]
        
public ListBox BoxRight {
            
get {
                
return lstRight;
            }
        }

        
private void btnAllToRight_Click(object sender, EventArgs e) {
            
if ( AllToRightClick != null ) {
                AllToRightClick(sender, e);
            }
        }

        
private void btnOneToRight_Click(object sender, EventArgs e) {
            
if ( OneToRightClick != null ) {
                OneToRightClick(sender, e);
            }
        }

        
private void btnOneToLeft_Click(object sender, EventArgs e) {
            
if ( OneToLeftClick != null ) {
                OneToLeftClick(sender, e);
            }
        }

        
private void btnAllToLeft_Click(object sender, EventArgs e) {
            
if ( AllToLeftClick != null ) {
                AllToLeftClick(sender, e);
            }
        }
        
// 控件改变大小时里面的控件也要相应改变
        private void ListBoxLR_Resize(object sender, EventArgs e) {
            
if ( this.Width<443 ) {
                
this.Width = 443;
            }
            
if ( this.Height<251 ) {
                
this.Height = 251;
            }

            
int width = this.Width - 4 * 15 - btnAllToRight.Width;
            
int height = this.Height - 4 * 15;

            lstLeft.Width 
= width / 2;
            lstLeft.Height 
= height;

            lstRight.Width 
= width / 2;
            lstRight.Height 
= height;

            lstRight.Left 
= 15 * 3 + width / 2 + btnAllToRight.Width;
            lblRight.Left 
= lstRight.Left;

            btnAllToRight.Left 
= 15 * 2 + lstLeft.Width;
            btnOneToRight.Left 
= btnAllToRight.Left;
            btnAllToLeft.Left 
= btnAllToRight.Left;
            btnOneToLeft.Left 
= btnAllToRight.Left;
        }

        
private void lstLeft_DoubleClick(object sender, EventArgs e) {
            
if ( OneToRightClick != null ) {
                OneToRightClick(sender, e);
            }
        }

        
private void lstRight_DoubleClick(object sender, EventArgs e) {
            
if ( AllToLeftClick != null ) {
                OneToLeftClick(sender, e);
            }
        }
    }
}
复制代码

 

 

点此下载

posted @   里沃特  阅读(1004)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示