C#里实现ComboBoxListBox

 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值