入门级: 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;
            }
        }

 

 

以下是完整源码:

 

 

ExpandedBlockStart.gif 完整源码
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);
            }
        }
    }
}

 

 

点此下载

转载于:https://www.cnblogs.com/lyout/archive/2010/09/20/1831633.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值