文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>入门级: WinForm 下的 ComboBox,ListBox 的使用 (三) 选择控件

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

时间:2010-09-20  来源:里奥特

平常我们经常会用到这样的控件:选中一个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     修改日期:
    /// 作者:夏荣全         修改人:
    /// 联系:[email protected] 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);
            }
        }
    }
}

 

 

点此下载

相关阅读 更多 +
排行榜 更多 +
瓢虫少女

瓢虫少女

飞行射击 下载
潜艇鱼雷

潜艇鱼雷

飞行射击 下载
网络掠夺者

网络掠夺者

飞行射击 下载