C#里实现ComboBoxListBox

本文介绍了一种在ComboBox控件中实现ListBox功能的方法,包括多选功能,并提供了具体的源码实现细节。通过创建一个继承自ComboBox的类,利用ListBox进行数据绑定及事件处理,实现了类似于ListBox的交互体验。

 ComboBoxListBox   在ComboBox控件里实现ListBox功能。如多选等。

ComboBoxListBox 源码:

public class ComboBoxListBox : ComboBox
    {
        private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
        ToolStripControlHost listBoxHost;
        ToolStripDropDown dropDown;
        private ListBox Lstbox = new ListBox();
        public ComboBoxListBox()
        {
        }
        public ListBox Set_ComboBoxListBox
        {
            set
            {
                Lstbox = value;
                Lstbox.SelectedItems.Clear();
                Lstbox.BorderStyle = BorderStyle.None;
                Lstbox.SelectedIndexChanged += new EventHandler(Lstbox_SelectedIndexChanged);
                Lstbox.SelectionMode = SelectionMode.MultiSimple;
                listBoxHost = new ToolStripControlHost(Lstbox);
                listBoxHost.AutoSize = false;
                dropDown = new ToolStripDropDown();
                dropDown.Width = this.Width;
                dropDown.Items.Add(listBoxHost);
            }
            get
            {
                return Lstbox;
            }
        }

        void Lstbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string StrSel = "";
            for (int i = 0; i < Lstbox.SelectedItems.Count; i++)
            {
                StrSel += ((System.Data.DataRowView)Lstbox.SelectedItems[i])[Lstbox.ValueMember] + ",";
            }
            if (!string.IsNullOrEmpty(StrSel))
            {
                this.Text = StrSel.Substring(0, StrSel.Length - 1);
            }
            else
            {
                this.Text = "";
            }
        }

        public CheckedListBox CheckedListBox
        {
            get { return listBoxHost.Control as CheckedListBox; }
        }
        private void ShowDropDown()
        {
            if (dropDown != null)
            {
                listBoxHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
                dropDown.Show(this, 0, this.Height);
            }
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
            {
                ShowDropDown();
                return;
            }
            base.WndProc(ref m);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (dropDown != null)
                {
                    dropDown.Dispose();
                    dropDown = null;
                }
            }
            base.Dispose(disposing);
        }
    }

用法:

            Ds 为 DataSet
            lst_Dept 为 在窗口里放置的一个ListBox
            cbolst_Dept 为 ComboBoxListBox

            lst_Dept.DataSource = Ds.Tables[0].DefaultView;
            lst_Dept.DisplayMember = "DeptName";
            lst_Dept.ValueMember = "DeptID";

            cbolst_Dept.Set_ComboBoxListBox = lst_Dept;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值