1、创建多选下拉框控件

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DFT_FFTApp.userCtrl
{
    /// <summary>
    /// 多选下拉控件
    /// </summary>
    public class MultiComboBox:UserControl
    {
        public ComboBox ComboBox { get; set; }
        public CheckedListBox CheckedListBox { get; set; }
        public ComboBox.ObjectCollection Items
        {
            get
            {
                return ComboBox?.Items;
            }
        }

        public MultiComboBox()
        {
            //面板
            this.VerticalScroll.Enabled = true;
            this.AutoSize = true;
            //多项列表
            CheckedListBox = new CheckedListBox();
            CheckedListBox.CheckOnClick = true;
            CheckedListBox.BorderStyle = BorderStyle.Fixed3D;
            CheckedListBox.Visible = false;
            CheckedListBox.Margin=new Padding(0);
            CheckedListBox.MouseUp += (ss, se) =>
            {
                //更新ComboBox显示文本
                var list = new List<string>();
                foreach(var item in CheckedListBox.CheckedItems)
                {
                    list.Add(item.ToString());
                }
                ComboBox.Text = string.Join(",", list);
                ComboBox.Tag = list;
            };
            CheckedListBox.MouseLeave += (ss, se) =>
            {
                //隐藏多选框
                CheckedListBox.Hide();
            };
            //下拉框
            ComboBox=new ComboBox();
            ComboBox.Width = 150;
            ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
            ComboBox.IntegralHeight = false;
            ComboBox.DroppedDown = false;
            ComboBox.DropDownHeight = 1;
            ComboBox.Margin=new Padding(0);
            ComboBox.Location=new System.Drawing.Point(0,0);
            ComboBox.DropDownStyle = ComboBoxStyle.DropDown;
            ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            ComboBox.MouseDown += (ss, se) =>
            {
                ComboBox.DroppedDown = false;
            };
            ComboBox.MouseLeave += (ss, se) =>
            {
                //不在下拉区时隐藏多项列表
                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();
                }
            };
            ComboBox.DropDown += (ss, se) =>
            {
                //显示下拉多选框
                CheckedListBox.Items.Clear();
                //添加并设置选中项
                var lastChecked = ComboBox.Tag as List<string>;
                ComboBox.BeginUpdate();
                foreach(var item in this.Items)
                {
                    var ck = false;
                    if(lastChecked!=null && lastChecked.Contains(item.ToString()))
                    {
                        ck = true;
                    }
                    CheckedListBox.Items.Add(item, ck);
                }
                //显示下拉框
                CheckedListBox.Width=ComboBox.Width;
                CheckedListBox.ItemHeight = ComboBox.ItemHeight;
                CheckedListBox.Size=new Size(ComboBox.DropDownWidth,this.Items.Count*18);
                CheckedListBox.Location = new Point(ComboBox.Left, ComboBox.Height);
                this.Controls.Add( CheckedListBox );
                CheckedListBox.Visible = true;
                ComboBox.EndUpdate();
            };
            //添加控件
            this.Controls.Add(ComboBox);
        }
    }
}

2、程序调用

using DFT_FFTApp.userCtrl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DFT_FFTApp
{
    public partial class MainForm : Form
    {
        MultiComboBox multiComboBox;
        public MainForm()
        {
            InitializeComponent();

            multiComboBox = new MultiComboBox();
            multiComboBox.Items.AddRange(new string[] { "AIN1", "AIN2", "AIN3", "AIN4" });
            multiComboBox.Location = new Point(175, 15);
            this.Controls.Add(multiComboBox);
        }

        private void Btn_ComboBoxTest_Click(object sender, EventArgs e)
        {
            //显示选择项
            CheckedListBox CheckedListBox =multiComboBox.CheckedListBox;
            foreach(var item in CheckedListBox.CheckedItems)
            {
                Console.WriteLine(item);
            }
            //显示选项列表
            //foreach (string item in multiComboBox.Items)
            //{
            //    Console.WriteLine(item);
            //}
        }
    }
}

3、运行结果
C#实现多选下拉框-LMLPHP

08-21 14:46