C#实现多选下拉框

1、创建多选下拉框控件

csharp 复制代码
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、程序调用

csharp 复制代码
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、运行结果

相关推荐
艾莉丝努力练剑6 分钟前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
一阵没来由的风6 分钟前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
CHEN5_0221 分钟前
【Java基础面试题】Java基础概念
java·开发语言
杜子不疼.2 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
落霞的思绪2 小时前
Java设计模式详细解读
java·开发语言·设计模式
阿巴~阿巴~2 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
java1234_小锋3 小时前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
甄超锋3 小时前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
凢en3 小时前
Perl——qw()函数
开发语言·perl
郝学胜-神的一滴4 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl