zoukankan      html  css  js  c++  java
  • 入门级: 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);
                }
            }
        }
    }

    点此下载

    版权声明 作者:夏荣全
    网站:http://www.lyout.com 邮箱:lyout(at)163.com
    扣扣:249775085
    本文版权归作者所有,转载请注明出处:http://www.cnblogs.com/lyout/
  • 相关阅读:
    在ASP.NET Core中怎么使用HttpContext.Current (转载)
    如何在.Net Core 2.0 App中读取appsettings.json
    ASP.NET CORE MVC 2.0 如何在Filter中使用依赖注入来读取AppSettings,及.NET Core控制台项目中读取AppSettings
    linux中shell变量$#,$@,$0,$1,$2的含义解释<转>
    ijkplayer阅读学习笔记之从代码上看播放流程
    ubuntu命令整理中
    Android SDK Android NDK Android Studio 官方下载地址<转>
    Ubuntu启动 卡在checking battery state 解决方案
    解决 ffmpeg 在avformat_find_stream_info执行时间太长
    ijkplayer阅读笔记系列<转>
  • 原文地址:https://www.cnblogs.com/lyout/p/1831633.html
Copyright © 2011-2022 走看看