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、运行结果

相关推荐
blammmp19 分钟前
Java:数据结构-枚举
java·开发语言·数据结构
何曾参静谧32 分钟前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
暗黑起源喵37 分钟前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong42 分钟前
Java反射
java·开发语言·反射
Troc_wangpeng43 分钟前
R language 关于二维平面直角坐标系的制作
开发语言·机器学习
努力的家伙是不讨厌的1 小时前
解析json导出csv或者直接入库
开发语言·python·json
Envyᥫᩣ1 小时前
C#语言:从入门到精通
开发语言·c#
童先生1 小时前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu1 小时前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
Re.不晚2 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea