首页 > 其他分享 >下拉多选框的开发

下拉多选框的开发

时间:2022-08-19 11:26:02浏览次数:52  
标签:下拉多 ComboBox void downArea var 开发 Items CheckedListBox 选框

在项目开发时,发现winform没有现成使用的下拉多选框,下例代码,亲测过,稳当可运行!!!!!

思路

基于原生控件ComboBoxCheckedListBox来实现,其中ComboBox负责显示多选结果和提供下拉按钮,然后CheckedListBox负责提供下拉多选操作

效果图如下所示:

 

 

 

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ComboBox.Width = 150;
            ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
            ComboBox.IntegralHeight = false;
            ComboBox.DroppedDown = false;
            ComboBox.DropDownHeight = 1;
            ComboBox.DropDownStyle = ComboBoxStyle.DropDown;
            ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
           // ComboBox.Items.AddRange(new string[] { "选项1 ", "选项2", "选项3", "选项4", "选项5" });


        }

        private void ComboBox_MouseDown(object sender, MouseEventArgs e)
        {
          ComboBox.DroppedDown = false;
        }

        private void ComboBox_MouseLeave(object sender, EventArgs e)
        {
            // 不在下拉区则隐藏下拉
            var curMousePos = this.PointToClient(Control.MousePosition);
            var downArea = CheckedListBox.Location;
            if (curMousePos.X < downArea.X || curMousePos.X > (downArea.X + CheckedListBox.Width)
            || curMousePos.Y < downArea.Y || curMousePos.Y > (downArea.Y + CheckedListBox.Height))
            {
                CheckedListBox.Hide();
            }
        }
        public ComboBox.ObjectCollection Items
        {
            get
            {
                return ComboBox?.Items;
            }
        }
        private void ComboBox_DropDown(object sender, EventArgs e)
        {
            // 显示下拉多选框
            CheckedListBox.Items.Clear();

            // 添加并设置选中项
            var lastChecked = ComboBox.Tag as List<string>;
            ComboBox.BeginUpdate();
            foreach (var v in this.Items)
            {
                var ck = false;
                if (lastChecked != null && lastChecked.IndexOf(v.ToString()) >= 0)
                {
                    ck = true;
                }
                CheckedListBox.Items.Add(v, ck);
            }
            // 显示下拉框
            CheckedListBox.Width = ComboBox.Width;
            CheckedListBox.ItemHeight = ComboBox.ItemHeight;
            CheckedListBox.Size = new Size(ComboBox.DropDownWidth, this.Items.Count * 18);//his.Items.Count * 18决定滚动条滚动大小
            CheckedListBox.Location = new Point(ComboBox.Left, ComboBox.Height+20);
            this.Controls.Add(CheckedListBox);
            CheckedListBox.Visible = true;
            ComboBox.EndUpdate();
        }

        private void CheckedListBox_MouseUp(object sender, MouseEventArgs e)
        {
            // 更新ComboBox显示文本
            var lst = new List<string>();
            foreach (var v in CheckedListBox.CheckedItems)
            {
                lst.Add(v.ToString());
            }
            ComboBox.Text = string.Join(",", lst);
            ComboBox.Tag = lst;
        }

        private void CheckedListBox_MouseLeave(object sender, EventArgs e)
        {
            // 隐藏下拉多选框
            CheckedListBox.Hide();
        }
    }

---------------------------有限的青春,敲写出唯美的code!!

标签:下拉多,ComboBox,void,downArea,var,开发,Items,CheckedListBox,选框
From: https://www.cnblogs.com/man520515/p/16601383.html

相关文章